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

Fix form method #1656

Merged
merged 3 commits into from Jun 26, 2015
Merged

Fix form method #1656

merged 3 commits into from Jun 26, 2015

Conversation

simov
Copy link
Member

@simov simov commented Jun 23, 2015

Fixes #1630

It turned out that when the body is set via the .form() method the content-length is not being set, and therefore the request is sent as transfer-encoding:'chunked' which may cause problems with some servers.

So I refactored out the logic responsible for setting up the content-length:

function setContentLength () {
  if (!Buffer.isBuffer(self.body) && !Array.isArray(self.body)) {
    self.body = new Buffer(self.body)
  }
  if (!self.hasHeader('content-length')) {
    var length = (Array.isArray(self.body))
      ? self.body.reduce(function (a, b) {return a + b.length}, 0)
      : self.body.length
    if (length) {
      self.setHeader('content-length', length)
    } else {
      self.emit('error', new Error('Argument error, options.body.'))
    }
  }
}

Much sexier than it was before, plus that conditional structure allows it to be called again inside the defer method. Now for the function name and declaration position it's just there for now, I couldn't figure out a better place to put it, but then again it's visible inside the defer function.

Lastly the test-form-urlencoded tests now checks if the content-length header is present as well.

simov added a commit that referenced this pull request Jun 26, 2015
@simov simov merged commit ef8c525 into request:master Jun 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant