Skip to content

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

@playnation81

Description

@playnation81

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions