0

I am developing an app for a course and I am having trouble getting photo upload to work. I didn't have much trouble with other endpoints, but this particular one has me stuck for a week now. Below I have copied the part I have issue with from the iNaturalist API reference.

POST /observation_photos

Auth required. Add photos to observations. This is only for iNat-hosted photos. For adding photos hosted on other services, see POST /observations and PUT /observations/:id.

Params

observation_photo[observation_id]
ID of the observation receiving this photo.
Allowed values: Valid iNat observation ID

file
The photo data.
Allowed values: Multipart photo data

On the front end, I have this form. I made sure I only uploading .png images.

 form(role='form', method='post', enctype='multipart/form-data')
   div.form-group
     input.form-control#image(type='file', accept='png', name='file')
   button.btn.btn-default(type='submit') Submit

Here is an excerpt of my code in the attempt to do a POST request following the API instructions.

var express = require('express');
var router = express.Router();
var Client = require('node-rest-client').Client;
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();

router.post('/playground', multipartMiddleware, function(req,res){
    client = new Client();

    var args = {
        parameters:{'observation_photo[observation_id]': '1055033', //id for a record
                    'file' : req.files.file},
        headers:{'Authorization': 'Bearer ' + token}
    }

    client.post('https://inaturalist.org/observation_photos',args, function(data,response){
        console.log(response.body);
        res.send(data);
    });
}

I guess the confusion I have is the file parameter and what form the POST endpoint is expecting for that parameter. Any help will be appreciated.

tailsx
  • 1
  • 3
  • Can you include the response you're getting from iNat? You also need to make sure that the request you're making to iNat is a [multipart request](http://stackoverflow.com/questions/16958448/what-is-http-multipart-request). If you're using https://github.com/aacerox/node-rest-client I'm not sure if it supports that, but it looks like https://github.com/danwrong/restler does. – kueda Nov 24 '14 at 23:50
  • yes it finally worked. Thank you. Probably should found this solution myself but glad I can move on now. – tailsx Dec 03 '14 at 05:34

0 Answers0