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

http body dropped when using delete method #19179

Closed
trevorlinton opened this issue Mar 6, 2018 · 3 comments
Closed

http body dropped when using delete method #19179

trevorlinton opened this issue Mar 6, 2018 · 3 comments
Labels
question Issues that look for answers.

Comments

@trevorlinton
Copy link

From what I can find, due to this line of code if you make an http request with a delete method the body will not be written (although no error will fire):

this.useChunkedEncodingByDefault = false;

From what I can find the spec does not forbid and http body being placed (nor discourages) it (however it may not necessarily be a very commonly used). Here's a test case showing/recreating the issue:

FAILS:

const http = require('http');
const url = require('url');
let req_method = 'delete'; // succeeds if you change this to post

let server = http.createServer((req, res) => {
  let method = req.method.toLowerCase();
  let uri = url.parse(req.url.toLowerCase());
  let data = Buffer.alloc(0);
  req.on('data', (datum) => data = Buffer.concat([data, datum]));
  req.on('end', () => {
    console.log(method, uri.path);
    console.log(data.toString('utf8'));
    console.assert(method === req_method);
    console.assert(data.toString('utf8') === 'Delete method should allow a body.');
    res.writeHead(200, 'Ok.');
    process.exit(1)
  })
}).listen(process.env.PORT || 5000, () => {
  let req = http.request({"method":req_method, "path":"/test/path", "host":"localhost", "port":5000});
  req.write('Delete method should allow a body.');
  req.end();
});

A work around is to add req.useChunkedEncodingByDefault = true before write or end. e.g.,

WORKAROUND:

const http = require('http');
const url = require('url');
let req_method = 'delete';

let server = http.createServer((req, res) => {
  let method = req.method.toLowerCase();
  let uri = url.parse(req.url.toLowerCase());
  let data = Buffer.alloc(0);
  req.on('data', (datum) => data = Buffer.concat([data, datum]));
  req.on('end', () => {
    console.log(method, uri.path);
    console.log(data.toString('utf8'));
    console.assert(method === req_method);
    console.assert(data.toString('utf8') === 'Delete method should allow a body.');
    res.writeHead(200, 'Ok.');
    process.exit(1)
  })
}).listen(process.env.PORT || 5000, () => {
  let req = http.request({"method":req_method, "path":"/test/path", "host":"localhost", "port":5000});
  req.useChunkedEncodingByDefault = true;
  req.write('Delete method should allow a body.');
  req.end();
});

Happy to submit a PR to see how i can help fix it, just wanted to make sure first there wasn't a disagreement as to whether delete should even have an option of having a body.

@devsnek
Copy link
Member

devsnek commented Mar 6, 2018

http spec says that while sending a body with DELETE is not forbidden (e.g. not a client error), a server should ignore it

section 4.3: "if the request method does not include defined semantics for an entity-body, then the message-body SHOULD be ignored when handling the request"

@apapirovski
Copy link
Member

apapirovski commented Mar 6, 2018

It's not a bug. As you've noticed, there's a property called useChunkedEncodingByDefault which is set to false by default for any of GET, HEAD, DELETE, OPTIONS and CONNECT. If you want to provide a body, you're free to do so but you have to use Content-Length or Transfer-Encoding: chunked to do so.

The fact that POST doesn't require this to be specified is just a convenience feature since it almost always comes with a body.

@apapirovski apapirovski added the question Issues that look for answers. label Mar 7, 2018
@apapirovski
Copy link
Member

I'll close this up as the question has been answered and there are no changes to be made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

3 participants