首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Mashape Imgur node.js图片上传

Mashape Imgur node.js图片上传
EN

Stack Overflow用户
提问于 2014-11-15 18:08:47
回答 2查看 988关注 0票数 3

正在尝试上载用户图像,但未成功。

HTML:

前端JS:

代码语言:javascript
运行
复制
            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:

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

代码语言:javascript
运行
复制
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);
        });
});

}

但我得到的只是:

代码语言:javascript
运行
复制
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 }

但是当使用这个curl测试auth时,它工作正常:

代码语言:javascript
运行
复制
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"

请问,unirest.post是什么?我需要提供更多信息吗?

EN

回答 2

Stack Overflow用户

发布于 2015-10-16 16:11:54

不幸的是,Mashape上的Node.js示例代码是错误的,您应该使用以下代码:

代码语言:javascript
运行
复制
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);
});
票数 4
EN

Stack Overflow用户

发布于 2014-12-09 01:49:15

您的客户端ID报头格式不正确(与cURL相比),如果您修复了它,它将正常工作

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26944694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档