Javascript Postman和使用superagent的简单Http请求之间的区别

Javascript Postman和使用superagent的简单Http请求之间的区别,javascript,node.js,postman,http-status-code-301,superagent,Javascript,Node.js,Postman,Http Status Code 301,Superagent,我想知道超级代理的简单POST请求和邮递员的POST请求有什么区别。 因为我想废弃一个网站,所以我向邮递员提出了一个发帖请求,一切都很顺利,我得到了预期的结果。但当我用superagent处理POST Http请求时,我得到了301重定向 有一种方法可以通过和邮递员一样的结果来解决这个问题吗 提前感谢您的回答。我不太清楚,但看起来邮递员遵循301(永久移动),而您的超级代理却没有。301是重定向响应。看 通常,您应该在代码中处理301响应。在回复中,您将找到重定向的URL。我记不清,但看起来邮递

我想知道超级代理的简单POST请求和邮递员的POST请求有什么区别。 因为我想废弃一个网站,所以我向邮递员提出了一个发帖请求,一切都很顺利,我得到了预期的结果。但当我用superagent处理POST Http请求时,我得到了301重定向

有一种方法可以通过和邮递员一样的结果来解决这个问题吗


提前感谢您的回答。

我不太清楚,但看起来邮递员遵循301(永久移动),而您的超级代理却没有。301是重定向响应。看


通常,您应该在代码中处理301响应。在回复中,您将找到重定向的URL。

我记不清,但看起来邮递员遵循301(永久移动),而您的超级代理没有。301是重定向响应。看


通常,您应该在代码中处理301响应。在响应中,您将找到重定向的URL。

您应该使用
重定向。简单的例子:

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

您应该使用
重定向
。简单的例子:

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

在postman中,若它得到HTTP 301响应,那个么它会自动重定向,若你们并没有看到301响应,那个么重定向后你们会得到一个实际的响应,但在superagent中,它不会自动重定向,你们会看到301响应

您可以在postman中启用拦截器来禁用自动重定向


大多数网站使用此标题向浏览器表示,它应该重新定向,例如,在某些网站中,如果您在postman中调用它get 301 response并重定向到

,如果它得到HTTP 301响应,则它将自动重定向,如果您看不到301响应,并且在重定向后在superagent中得到实际响应,它不会自动重定向,您会看到301的响应

您可以在postman中启用拦截器来禁用自动重定向


大多数网站使用此标题对浏览器说,它应该重新访问。例如,在某些网站中,如果您称其为get 301 response并重定向到

感谢各位的回答,我可能会发现我的问题:事实上,这是服务器等待应用程序/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)
        })
    })
}
好的,我找到了解决方案,对于其他人,我在上面发布了以下内容:

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

谢谢你们的回答,我可能发现了我的问题:实际上是服务器的等待应用程序/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)
        })
    })
}
好的,我找到了解决方案,对于其他人,我在上面发布了以下内容:

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

Paul,请在StackOverflow的下一个问题中添加代码。Paul,请在StackOverflow的下一个问题中添加代码。