Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not send content-length header when methodRewriting is false #1406

Closed
2 tasks done
playnation81 opened this issue Aug 17, 2020 · 2 comments
Closed
2 tasks done

Do not send content-length header when methodRewriting is false #1406

playnation81 opened this issue Aug 17, 2020 · 2 comments
Labels
bug Something does not work as it should ✭ help wanted ✭
Milestone

Comments

@playnation81
Copy link

Describe the bug

  • Got version: 11.5.2
  • Node.js version: 14.8.0
  • OS & version: Windows 10 Pro 1909

Actual behavior

Empty body request with header[content-length > 0] was sent when got a 302(Found) response status code after submitting POST request.

Expected behavior

Send empty body request without header[content-length] when got a 302(Found) response status code after submitting POST request.

Additional Info

Content-Length (if required and present) must be the size of the body of the request.
Which was mentioned by @Giotino in #1258 (comment)_

Code to reproduce

const got = require('got');

(async () => {
  try {
    const res = await got({
      url: `http://httpstat.us/302`,
      method: 'POST',
      methodRewriting: false,
      form: { aaa: 'bbb' },
    });
  } catch (err) {
    console.log(err);
  }
})();

Temporary solution

Adding a hooks.beforeRedirect to delete the Content-Length header.

const got = require('got');

(async () => {
  try {
    const res = await got({
      url: `http://httpstat.us/302`,
      method: 'POST',
      methodRewriting: false,
      form: { aaa: 'bbb' },
      hooks: {
        beforeRedirect: [
          (options, response) => {
            options.headers['content-length'] &&
              delete options.headers['content-length'];
          },
        ],
      },
    });
  } catch (err) {
    console.log(err);
  }
})();

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.
@szmarczak
Copy link
Collaborator

A user agent SHOULD send a Content-Length in a request message when

no Transfer-Encoding is sent and the request method defines a meaning
for an enclosed payload body. For example, a Content-Length header
field is normally sent in a POST request even when the value is 0
(indicating an empty payload body).

https://tools.ietf.org/html/rfc7230#section-3.3.2

@playnation81
Copy link
Author

Sorry, I forgot to mention that I disabled the methodRewriting option.
Which means the redirected request would be a GET request and the body has been removed.

The body is removed if you disable method rewriting.
Originally posted by @szmarczak in #1271 (comment)

A user agent SHOULD NOT send a
Content-Length header field when the request message does not contain
a payload body and the method semantics do not anticipate such a
body.

https://tools.ietf.org/html/rfc7230#section-3.3.2

@szmarczak szmarczak reopened this Aug 17, 2020
@szmarczak szmarczak added bug Something does not work as it should ✭ help wanted ✭ labels Aug 17, 2020
@szmarczak szmarczak changed the title Empty body request with content-length > 0 was sent when redirected after submitting POST request Do not send content-length header when methodRewriting is false Aug 17, 2020
@szmarczak szmarczak added this to the Got v11.7.0 milestone Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something does not work as it should ✭ help wanted ✭
Projects
None yet
Development

No branches or pull requests

2 participants