请求遵循重定向而不发布数据 [英] request follow redirection without post data

查看:235
本文介绍了请求遵循重定向而不发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 request.js 获取Node.js中的http请求。

I'm using request.js for http request in Node.js.

request({
  method: 'POST',
  uri: 'http://www.example.com/getData',
  followAllRedirects: true,
  followOriginalHttpMethod: true,
  json: true,
  form: {
    id: 1,
    msg: 'Test data',
  },
}, (err, res, body) => {})

让我们说当我将 {id:1,msg:'测试数据'} 发布到 http:// www。 example.com/getData ,代理将重定向到 http://us.example.com/getData

Let's say when I post { id: 1, msg: 'Test data' } to http://www.example.com/getData, proxy will redirect to http://us.example.com/getData.

上面的代码会重定向到 http://us.example.com/getData ,但如何设置重定向和帖子数据?

The codes above do redirect to http://us.example.com/getData, but How can I set the redirection with post data?

推荐答案

如果不是不可能的话,这很难做到。

This will be hard to do if not impossible.

如果重定向是通过发送301 Moved Permanently或30 2找到HTTP状态代码然后在实践中它就像发送了303 See Other一样,即 GET请求被设置为而没有在正文中发送数据

If the redirects are done by sending "301 Moved Permanently" or "302 Found" HTTP status code then in practice it works as if it the "303 See Other" was sent, i.e. a GET request is made with no data sent in the body.

请参阅 HTTP状态代码列表


这是与标准相矛盾的行业惯例的一个例子。
HTTP / 1.0规范(RFC 1945)要求客户端执行
临时重定向(原始描述短语为暂时移动
),但流行的浏览器用$ b实现302 $ b的功能303见其他。因此,HTTP / 1.1添加状态
代码303和307来区分这两种行为。
但是,一些Web应用程序和框架使用302状态代码
,就像它是303一样。

This is an example of industry practice contradicting the standard. The HTTP/1.0 specification (RFC 1945) required the client to perform a temporary redirect (the original describing phrase was "Moved Temporarily"), but popular browsers implemented 302 with the functionality of a 303 See Other. Therefore, HTTP/1.1 added status codes 303 and 307 to distinguish between the two behaviours. However, some Web applications and frameworks use the 302 status code as if it were the 303.

有一个307 Temporary Redirect(自HTTP / 1.1以来)创建来解决这个问题不允许更改HTTP方法 - 所以POST的重定向应该仍然是POST - 请参阅Wikipedia:

There is a "307 Temporary Redirect" (since HTTP/1.1) created to address this issue that is not allowed to change the HTTP method - so a redirect from POST should still be POST - see Wikipedia:


在这种情况下,请求应该使用另一个URI重复;
但是,未来的请求仍应使用原始URI。在
中,与历史上实现302的方式形成对比,在重新发出原始请求时,不允许更改请求方法
。对于
示例,应使用另一个POST
请求重复POST请求。

In this case, the request should be repeated with another URI; however, future requests should still use the original URI. In contrast to how 302 was historically implemented, the request method is not allowed to be changed when reissuing the original request. For example, a POST request should be repeated using another POST request.

但如果这样可行在这种特殊情况下很难说,因为它取决于您正在调用的API,并且需要更改该API实现。

But if that would work in that particular case is hard to tell as it depends on the API that you're calling and would require changes to that API implementation.

这篇关于请求遵循重定向而不发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆