【问题标题】:How to make https calls using SuperAgent?如何使用 SuperAgent 进行 https 调用?
【发布时间】:2016-03-05 08:12:00
【问题描述】:

我在类似于 Node.js 环境的 React Native Android 中使用 SuperAgent。我正在尝试使用 https 协议调用我的 API。但是,只需调用

Req = SuperAgent
        .get(‘https://url...')
        .set('Accept','application/json')
        .end(function(err, res){some code})

返回未找到 res 的错误。我无法在官方文档中找到 https 呼叫指南。非常感谢您的帮助!

【问题讨论】:

标签: javascript node.js https react-native superagent


【解决方案1】:

一个最简单的例子

var request = require('superagent');
 //an example for Get
request
    .get(example_url) //give the url
    .set('Cookie', 'hello') //setting cookie
    .set('user-agent', 'Android') //setting UserAgent
    .end(function(error,res){
      /* handle the Response(res) or Error (err) */
    }.bind(this)); //bind is basically used when we use this inside end for setState or any other scenario (basically use of this)

//Example for POST

和上面一样,只是东西不是 GET 你需要将它声明为 POST 并连同它一起发送数据

以及其他东西的使用可以参考下面SuperAgent的lib https://www.npmjs.com/package/superagent 要么 https://github.com/visionmedia/superagent

【讨论】:

  • 对,我只要把http改成https就好了。也感谢您的 bind(this) 建议。
猜你喜欢
  • 2016-11-13
  • 2017-08-31
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
  • 2014-04-10
  • 1970-01-01
  • 2014-02-01
  • 1970-01-01
相关资源
最近更新 更多