Imgur通过客户端javascript而不是node.js上载工作. Flummoxed [英] Imgur uploads working via client-side javascript but not node.js. Flummoxed

查看:87
本文介绍了Imgur通过客户端javascript而不是node.js上载工作. Flummoxed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

    $.post( 
        'http://api.imgur.com/2/upload.json', 
        { 
            key: 'dsfdwerwersf53534dfsfse3', 
            image: 'http://i.imgur.com/UH17u.png' 
        }, 
        function( data ) { console.log( data.upload.links.original ); }
    );

当我在Chrome的控制台中运行此代码时,imgur会立即向我发送上传图像的网址:http://i.imgur.com/KrHMY.png.很棒.

When I run this in Chrome's console, imgur sends me the url of the uploaded image right away: http://i.imgur.com/KrHMY.png. Excellent.

.

现在,我正在使用node.js在服务器端尝试相同的操作,但是它不起作用:

Now I'm try the same thing on the server-side using node.js but it doesn't work:

    var 
        express = require( 'express' )
    ,   app = express.createServer()
    ,   request = require( 'superagent' );


    app.get( '/', function( req, res ) {
        request
            .post( 'http://api.imgur.com/2/upload.json' )
            .send( { key: 'dsfdwerwersf53534dfsfse3', image: 'http://i.imgur.com/UH17u.png' } )
            .end( function( data ) {
                console.log( data.upload.links.original );
            });
    });

    app.listen( '8080' );

当我运行node.js应用程序时,imgur会发回此响应:

When I run the node.js app, imgur sends back this response:

text: '{"error":{"message":"No API key was sent, and no user is authenticated","request":"\\/2\\/upload.json","method":"post","format":"json","parameters":"image = {\\"key\\":\\"dsfdwerwersf53534dfsfse3\\",\\"image\\":\\"http:\\/\\/i.imgur.com\\/UH17u.png\\"}"}}',

关键部分是这个:No API key was sent.为什么imgur不能识别我与POST一起发送的key?

The key part is this: No API key was sent. Why isn't imgur recognizing the key I'm sending with the POST?

我尝试同时使用requestsuperagent模块.我很傻,无法弄清楚为什么这么简单的东西行不通.感谢任何指针.

I've tried using both request and superagent modules. I feel silly not being able to figure out why something so simple isn't working. Appreciate any pointers.

推荐答案

jQuery在某些时候非常庞大,以至于很难看到它.

jQuery does sometime so much magie that it is hard to see it.

在这里我也打赌两件事,我一个月没有使用超级代理,并且我有像这样的愚蠢错误,使我感到难过.

Here I'd bet two thing either, I have not used superagent in month and I had stupid error like this one that made me sad.

但是在这里我敢打赌,主体最多是一个要串成json或您的对象的字符串. 就像大多数API希望POST正文一样,默认情况下urlencode是JQuery. 使用

But here I bet that the body is a to string at best a json or your object. JQuery by default urlencode just like most API want the POST body to be. Use

var = require('querystring');
/*...*/
.send(qs.encode({ key: '...', image: '...' }))

这篇关于Imgur通过客户端javascript而不是node.js上载工作. Flummoxed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆