Skip to content

http body dropped when using delete method #19179

@trevorlinton

Description

@trevorlinton

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions