Skip to content

Better error message when request has incorrect content-length #11524

Closed
@StevenWinde

Description

@StevenWinde

Subsystem: http - ClientRequest

I was making a request that had a string with a special character. In a buffer the character's size is 3 instead of the regular 1. The request would fail with a socket hang up and it took awhile to figure out why the request was failing. If there could be a more detailed error message for when there is a mismatch between the content-length and the actual payload that would be appreciated.

Simplest Example to Reproduce

const http = require( 'http' );

const requestData = '–';

http.request( {
  headers: {
    accept:           'application/json',
    'content-type':   'application/json',
    'content-length': requestData.length
  },
  host:   'localhost',
  port:   3000,
  path:   '/foo',
  method: 'post'
}, ( res )=>{

  const accResponseBody = [];

  res
    .on( 'data', console.log )
    .on( 'error', console.log );

} )
  .on( 'error', console.log )
  .end( requestData );

Expected Behavior

{ Error: Incorrect content-length }

Current Behavior

{ Error: socket hang up
    at createHangUpError (_http_client.js:302:15)
    at Socket.socketOnEnd (_http_client.js:394:23)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:186:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9) code: 'ECONNRESET' }

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpIssues or PRs related to the http subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions