最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parse.Cloud.httpRequest call with HTTP request header - Stack Overflow

programmeradmin8浏览0评论

I have a question for cloud code function "Parse.Cloud.httpRequest". I want to send HTTP GET request as the same as the following curl mand. But it seems it is not working. If you find something wrong, please help.

curl -H "Authorization: token xxx" ""

Note:

My code is like this. Then I accessed /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: '',
        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", "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

I have a question for cloud code function "Parse.Cloud.httpRequest". I want to send HTTP GET request as the same as the following curl mand. But it seems it is not working. If you find something wrong, please help.

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

Note:

My code is like this. Then I accessed /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./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.", "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
Share Improve this question edited Jul 17, 2014 at 17:07 Yohei Onishi asked Jul 16, 2014 at 5:05 Yohei OnishiYohei Onishi 1,5241 gold badge26 silver badges50 bronze badges 7
  • 1 is it just a typo for this post that the url starts with 'ttps' and not 'https' ? – Fosco Commented Jul 16, 2014 at 5:41
  • Thanks. There is limitation for URL, so I changed it. But now it is fine. – Yohei Onishi Commented Jul 16, 2014 at 17:30
  • so what is returned? don't just log .status, log everything. – Fosco Commented Jul 16, 2014 at 17:33
  • No return. Even successful or error function is not called. – Yohei Onishi Commented Jul 16, 2014 at 20:23
  • is there more code after this httpRequest call, outside the block?.. – Fosco Commented Jul 16, 2014 at 20:28
 |  Show 2 more ments

2 Answers 2

Reset to default 8

Try this modification:

app.get('/trips', function(req, res) {
    Parse.Cloud.httpRequest({
      url: 'https://api.automatic./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:

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./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. HTTPRequest {"code":141,"error":"success/error was not called"}

发布评论

评论列表(0)

  1. 暂无评论