使用HTTP请求标头调用Parse.Cloud.httpRequest

时间:2014-07-16 05:05:03

标签: javascript http curl parse-platform

我对云代码功能有一个问题" Parse.Cloud.httpRequest"。我想发送HTTP GET请求与以下curl命令相同。但它似乎无法正常工作。如果您发现错误,请提供帮助。

curl -H "Authorization: token xxx" "https://api.automatic.com/v1/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();

这是一个日志。

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

2 个答案:

答案 0 :(得分:8)

尝试此修改:

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);
    });
});

答案 1 :(得分:0)

@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();

请参阅:Parse.com HTTPRequest {"code":141,"error":"success/error was not called"}