Postman和一个简单的Http请求之间的差异与superagent

我想知道一个简单的POST请求与superagent和POST请求的POST请求之间有什么区别。 因为我试图报废一个网站,所以我向邮递员发了一个邮件请求,一切正常,我得到了预期的结果。 但是当我用superagent进行POST Http请求时,我得到了301redirect。

有一种方法可以通过这个问题来得到和邮差一样的结果吗?

预先感谢您的回答。

你应该使用redirects 。 简单的例子:

 const request = require('superagent'); const url = 'localhost:3000/example'; request .post(url) .send({msg: "hello"}) .redirects(1) //Add redirect functionality .on('redirect', function(res) { console.log('Redirected'); }) .end(function(err, res){ console.log(res); }) 

我不知道的心,但它看起来像邮递员301(永久移动),你的超级特工是不是。 301是redirect响应。 查看详情

通常你应该在你的代码中处理301个响应。 在回复中你会findredirect的URL。

在邮递员,如果它得到了HTTP 301响应,它将自动redirect,如果你没有看到301响应,你会得到一个实际的响应后redirect,但在superagent,它不会自动redirect,你看到301响应。

你可以启用拦截器来禁用邮递员自动redirect。

大多数网站使用这个头文件说,它应该重新启动例如在某些网站,如果你打电话http://target.com它得到301响应和redirect到https://target.com

感谢你们的答案,我可能会发现我的问题:它实际上是服务器等待application / x-www-form-urlencoded格式。 在这里输入图像描述

如何在superagent HTTP请求中转换此内容? 我试过了 :

 postData() { return new Promise((resolve, reject) => { request .post(this.url) .send('destinations={"1": "testa"}') .send('stopId=2643') .send('lineId=1150') .send('sens=2') .end((err, res) => { if (err) reject(err); resolve(res) }) }) } 

好的,我find了解决scheme,对于其他人,我张贴以上内容:

 request .post(this.url) .set('Content-Type', 'application/x-www-form-urlencoded') .send({destinations: '{"1":"test"}'}) .send({stopId: "2643"}) .send({lineId: "1150"}) .send({sens: "2"}) .end(function(err, res){ console.log(res); })