Posting photos using the API

Hello I am trying to use the INaturalist API to post photos in my observations but it’s giving me this error:

b’{“errors”:“No observation_photo specified”}’

Can anyone explain how do I fix the following python code?

def post_observation_photos():

  • im = open(filepath, “rb”).read()
    im_b64 = base64.b64encode(im).decode(“utf8”)
    head = {
    “Content-Disposition”: “form-data”,
    ‘Authorization’: 'Bearer ’ + access_token}
    payload = {
    “observation_id”: obs_id
    }
    files = {“observation_photo”: im}
    response = requests.post(url + “/observation_photos”, allow_redirects=True, json=payload, headers=head, data=files)

Thank you for your time :)

1 Like

since you’re using Python, you should know that pyinaturalist exists. you could use it for development or look at what it does to handle file upload. @jcook works on pyinaturalist.

2 Likes

If you’re using requests, the easiest way to handle file uploads is by using the files argument instead of data:
https://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file

You could also use pyinaturalist.upload:

from pyinaturalist import get_access_token, upload

access_token = get_access_token(username, password, app_id, app_secret)
upload(obs_id, photos=filepath, access_token=access_token)
2 Likes

First, thanks for the quick replies they were very useful.

My problem was solved by updating some out-of-date libraries.

Sorry for disturbing you.

2 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.