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

Sending requests with content-length equal to zero raises an error #920

Closed
forw opened this issue Jun 4, 2014 · 5 comments · May be fixed by #3084
Closed

Sending requests with content-length equal to zero raises an error #920

forw opened this issue Jun 4, 2014 · 5 comments · May be fixed by #3084
Labels

Comments

@forw
Copy link

forw commented Jun 4, 2014

In the request.js file there is a check:
if (length) {
if (!self.hasHeader('content-length')) self.setHeader('content-length', length)
} else {
throw new Error('Argument error, options.body.')
}
which prevents sending files with zero length. I understand this not very important issue, but still. Specification says "Any Content-Length greater than or equal to zero is a valid value." (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)

I faced with this restriction when using cradle which we use to save user files to CouchDB and some of files have zero length (CouchDB also accepts empty files), so the problem is caused only by the request module.

@normalser
Copy link

Please address this issues. We are using https://github.com/google/google-api-nodejs-client which is using request and we have proper use case where we need to create file in the bucket with empty content. Right now it's not possible due to above issue.

@hughsk
Copy link

hughsk commented Dec 16, 2014

+1

1 similar comment
@kavehfa
Copy link

kavehfa commented Mar 30, 2016

+1

@ghost
Copy link

ghost commented Nov 8, 2018

Bug: a zero-length body causes a false error.

request/request.js

Lines 419 to 440 in 8162961

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

Hold on; over 4 years' passed, yet still no fix for the single irresponsible line if(length)? :smh: I cannot PUT my empty files. Is this really how it's supposed to be? I'm pretty sure it isn't.

Workaround for you guys

if (!self.hasHeader('content-length')) {

request/request.js

Lines 141 to 149 in 8162961

Request.prototype.init = function (options) {
// init() contains all the code to setup the request object.
// the actual outgoing request is not started until start() is called
// this function is called from both the constructor and on redirect.
var self = this
if (!options) {
options = {}
}
self.headers = self.headers ? copy(self.headers) : {}

Apparently, it doesn't filter a zero-length body out when Content-Length header's set. Is it a sort of safety lock?

await request.put({ // I'm using “request-promise-native” module.
	uri: "http://localhost/" + destination_file_path,
	qs: {timestamp, auth_code},
	headers: {
		"Content-Type": "application/octet-stream",
		"Content-Length": file_data.length
	},
	body: file_data
});

pretorh added a commit to pretorh/forked-request that referenced this issue Dec 10, 2018
fix check for if length is set in order to allow passing a 0-length
buffer as body
@stale
Copy link

stale bot commented Nov 15, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 15, 2019
@stale stale bot closed this as completed Nov 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants