Postman 和带有 superagent 的简单 Http 请求之间的区别
Difference between Postman and a simple Http Request with superagent
我想知道使用 superagent 的简单 POST 请求和使用 Postman 的 POST 请求之间有什么区别。
因为我试图废弃一个网站,所以我向 Postman 发出了 post 请求,一切正常,我得到了预期的结果。但是当我使用 superagent 进行 POST Http 请求时,我得到了 301 重定向。
有没有办法解决这个问题,得到与 Postman 相同的结果?
预先感谢您的回答。
我不太清楚,但看起来邮递员遵循 301(永久移动)而您的超级代理不是。 301 是重定向响应。参见 details
通常您应该在代码中处理 301 响应。在响应中,您会发现重定向的 URL.
你应该使用 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);
})
在邮递员中,如果它收到 HTTP 301 响应,它会自动重定向,如果您没有看到 301 响应,并且在重定向后得到实际响应,但在 superagent 中,它不会自动重定向,您会看到 301 响应.
您可以启用拦截器以禁用 postman 中的自动重定向。
大多数网站使用此 header 告诉浏览器它应该重定向,例如在某些网站中,如果您调用 http://target.com it get 301 response and redirect to 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)
})
})
}
好的,我找到了解决方案,对于其他人,我 post 以上:
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);
})
我想知道使用 superagent 的简单 POST 请求和使用 Postman 的 POST 请求之间有什么区别。 因为我试图废弃一个网站,所以我向 Postman 发出了 post 请求,一切正常,我得到了预期的结果。但是当我使用 superagent 进行 POST Http 请求时,我得到了 301 重定向。
有没有办法解决这个问题,得到与 Postman 相同的结果?
预先感谢您的回答。
我不太清楚,但看起来邮递员遵循 301(永久移动)而您的超级代理不是。 301 是重定向响应。参见 details
通常您应该在代码中处理 301 响应。在响应中,您会发现重定向的 URL.
你应该使用 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);
})
在邮递员中,如果它收到 HTTP 301 响应,它会自动重定向,如果您没有看到 301 响应,并且在重定向后得到实际响应,但在 superagent 中,它不会自动重定向,您会看到 301 响应.
您可以启用拦截器以禁用 postman 中的自动重定向。
大多数网站使用此 header 告诉浏览器它应该重定向,例如在某些网站中,如果您调用 http://target.com it get 301 response and redirect to 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)
})
})
}
好的,我找到了解决方案,对于其他人,我 post 以上:
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);
})