简体   繁体   English

具有HTTP请求标头的Parse.Cloud.httpRequest调用

[英]Parse.Cloud.httpRequest call with HTTP request header

I have a question for cloud code function "Parse.Cloud.httpRequest". 我对云代码功能“ Parse.Cloud.httpRequest”有疑问。 I want to send HTTP GET request as the same as the following curl command. 我想发送与以下curl命令相同的HTTP GET请求。 But it seems it is not working. 但是似乎没有用。 If you find something wrong, please help. 如果发现问题,请提供帮助。

curl -H "Authorization: token xxx" "https://api.automatic.com/v1/trips"

Note: 注意:

My code is like this. 我的代码是这样的。 Then I accessed /trips. 然后我访问了/ trips。

var express = require('express');
var app = express();

app.set('views', 'cloud/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());

app.get('/trips', function(req, res) {
    Parse.Cloud.httpRequest({
        url: 'https://api.automatic.com/v1/trips',
        headers: {
            'Authorization': 'token xxx'
        },
        success: function (httpResponse) {
            console.log(httpResponse.text);
        },
        error: function (httpResponse) {
            console.error('Request failed with response code ' + httpResponse.status);
        }
    });
});

app.listen();

Here is a log. 这是一条日志。

E2014-07-16T04:10:46.102Z] v170: Ran custom endpoint with:
Input: {"method"=>"GET", "url"=>"/trips", "headers"=>{"version"=>"HTTP/1.1",   "host"=>"easyparking.parseapp.com", "user-agent"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36", "accept"=>"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "accept-encoding"=>"gzip,deflate,sdch", "accept-language"=>"ja,en-US;q=0.8,en;q=0.6", "cache-control"=>"max-age=0", "x-forwarded-proto"=>"http"}}
Result: success/error was not called

Try this modification: 试试这个修改:

app.get('/trips', function(req, res) {
    Parse.Cloud.httpRequest({
      url: 'https://api.automatic.com/v1/trips',
      headers: {
        'Authorization': 'token xxx'
      }
    }).then(function(httpResponse) {
      console.log(httpResponse);
      res.end(httpResponse.text);
    }, function(err) {
      console.log(err);
      res.end(err);
    });
});

@Yohei, I believe you could have also done this: @Yohei,我相信你也可以这样做:

var express = require('express');
var app = express();

app.set('views', 'cloud/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());

app.get('/trips', function(req, res) {
    Parse.Cloud.httpRequest({
        url: 'https://api.automatic.com/v1/trips',
        headers: {
            'Authorization': 'token xxx'
        },
        success: function (httpResponse) {
            console.log(httpResponse.text);
            response.success(httpResponse.text);
        },
        error: function (httpResponse) {
            console.error('Request failed with response code ' + httpResponse.status);
            response.error('Request failed with response code ' + httpResponse.status);
        }
    });
});

app.listen();

See: Parse.com HTTPRequest {"code":141,"error":"success/error was not called"} 请参阅: Parse.com HTTPRequest {“代码”:141,“错误”:“未成功/错误”“}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM