Mashape Imgur node.js image upload

1k views Asked by At

Trying to upload users image but without success.

HTML:

Front-End JS:

            var fileInput = document.getElementById('gg_test_input');


            fileInput.addEventListener('change', function(e) {
                var file =  fileInput.files[0];
                var xhr = new XMLHttpRequest();
                var formData = new FormData();


                formData.append("file", file);
                xhr.open('POST', '/gg_upload');
                xhr.send(formData);
            });

Node.js that we sending a request to:

function(req, res){ var form = new formidable.IncomingForm();

form.parse(req, function(err, fields, files) {
    if( err ) throw err;


    unirest.post('http://httpbin.org/post')
        .header("X-Mashape-Key", "MASHAPE_KEY")
        .header("Authorization", "clientID_KEY")
        .attach('file', files.file.path)
        .end(function (response) {
            console.log(response.status, response.headers, response.body);
        });
});

}

But I get just:

403 { 'access-control-allow-headers': 'Authorization, Content-Type, Accept, X-Mashape-Authorization',
  'access-control-allow-methods': 'GET, PUT, POST, DELETE, OPTIONS',
  'access-control-allow-origin': '*',
  'cache-control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
  'content-type': 'application/json',
  date: 'Sat, 15 Nov 2014 10:01:20 GMT',
  etag: '"********************************************"',
  server: 'Mashape/5.0.5',
  'x-ratelimit-requests-limit': '12500',
  'x-ratelimit-requests-remaining': '12468',
  'x-ratelimit-uploads-limit': '1250',
  'x-ratelimit-uploads-remaining': '1248',
  'content-length': '110',
  connection: 'keep-alive' } { data: 
   { error: 'Malformed auth header',
     request: '/3/image',
     method: 'POST' },
  success: false,
  status: 403 }

But when tested auth with this curl it is working OK:

curl -X POST --include "https://imgur-apiv3.p.mashape.com/3/image" -H "X-Mashape-Key: MASHAPE_KEY" -H "Authorization: Client-ID clientID_KEY" -F "image=@/home/user/Desktop/face.jpg"

PLEASE, what would be the unirest.post ? Do I need provide more information?

2

There are 2 answers

0
API_sheriff_orlie On

You Client ID header is malformed (compare with cURL), if you fix that then it will work

1
Gergo On

Unfortunately the Node.js example code on Mashape is wrong, you should use this:

var unirest = require('unirest');

unirest.post("https://imgur-apiv3.p.mashape.com/3/image")
.header("X-Mashape-Key", "MASHAPE_KEY")
.header("Authorization", "Client-ID CLIENT_ID")
.header("Content-Type", "multipart/form-data")
.attach("image", "/Users/example/Projects/imgur/test_image.jpeg")
.end(function (result) {
  console.log(result.status, result.headers, result.body);
});