Kong / unirest-nodejs

Unirest in Node.js: Simplified, lightweight HTTP client library.
http://unirest.io/nodejs
MIT License
951 stars 167 forks source link

TypeError [ERR_HTTP_INVALID_HEADER_VALUE]: Invalid value "undefined" for header "Content-Length" #127

Open Daniyal83 opened 5 years ago

Daniyal83 commented 5 years ago

Hello. When I use unirest to make a request a get an error ' Invalid value "undefined" for header "Content-Length" '. How can I prevent that? It happens from time to time, but not every time. I'm using node.js on backend for my application. Here is my function

function addTasks(object) {
    return new Promise((resolve, reject) => {
        unirest.post(`${xtmBaseUrl}/rest-api/projects`)
        .headers({"Authorization": xtmToken,
        'Content-Type': 'multipart/form-data'})  
        .field('customerId', object.customerId)
        .field('name', object.name)
        .field('sourceLanguage', object.source)
        .field('targetLanguages', object.target)
        .field('analysisTemplateId', object.templateId)
        .field(withJoinObject)
        .attach(filesObj)
        .end(response => {
            if(response.error) {
                reject(response.error)
            }
            resolve(response.body)
        })
    })
}
AntoTG commented 5 years ago

Hello @Daniyal83 I had the same problem, my solution was update the form-data dependency of unirest-nodejs to the last version (2.5.1) on the package.js of unirest-nodejs

bambita commented 4 years ago

I am facing the same error. I use "unirest": "^0.6.0" and "form-data": "^3.0.0" Any idea?

adishupadhyay commented 4 years ago

Hi All , Any update on above mentioned issue? I am facing same problem

adishupadhyay commented 4 years ago

Hello @Daniyal83 I had the same problem, my solution was update the form-data dependency of unirest-nodejs to the last version (2.5.1) on the package.js of unirest-nodejs

How you updated, its internally used by unirest

jbw09 commented 2 years ago

Still getting this error with unirest 0.6.0 and form-data 4.0.0 while trying to upload attachments in Freshdesk. Most annoying part is I can't figure out how to catch it so it keeps stopping program execution -

try {
        unirest.put(url)
        .headers(headers)
        .attach('attachments[]', fs.createReadStream('./FDattachments/' + encodeURIComponent(attachElement.name)))
        .end(function(response){
            //console.log(response.body)
            console.log(response.headers)
            console.log("Response Status : " + response.status)
            if(response.status == 201){
                console.log("Location Header : "+ response.headers['location'])
            }
            else{
                console.log("X-Request-Id :" + response.headers['x-request-id']);
            }
        });
    } catch (err) {
        console.log(err)
    }

Error thrown -

PS C:\Users\jaywi\Downloads\SW Dev\Heroku Node JS\CSQ> node migratemoretest.js > migrate_output.txt node:_http_outgoing:566 throw new ERR_HTTP_INVALID_HEADER_VALUE(value, name); ^

TypeError [ERR_HTTP_INVALID_HEADER_VALUE]: Invalid value "undefined" for header "Content-Length" at ClientRequest.setHeader (node:_http_outgoing:579:3) at FormData. (C:\Users\jaywi\Downloads\SW Dev\Heroku Node JS\CSQ\node_modules\unirest\node_modules\form-data\lib\form_data.js:321:13) at C:\Users\jaywi\Downloads\SW Dev\Heroku Node JS\CSQ\node_modules\unirest\node_modules\form-data\lib\form_data.js:265:7 at C:\Users\jaywi\Downloads\SW Dev\Heroku Node JS\CSQ\node_modules\unirest\node_modules\async\lib\async.js:251:17 at done (C:\Users\jaywi\Downloads\SW Dev\Heroku Node JS\CSQ\node_modules\unirest\node_modules\async\lib\async.js:126:15) at C:\Users\jaywi\Downloads\SW Dev\Heroku Node JS\CSQ\node_modules\unirest\node_modules\async\lib\async.js:32:16 at C:\Users\jaywi\Downloads\SW Dev\Heroku Node JS\CSQ\node_modules\unirest\node_modules\async\lib\async.js:248:21 at C:\Users\jaywi\Downloads\SW Dev\Heroku Node JS\CSQ\node_modules\unirest\node_modules\async\lib\async.js:572:34 at C:\Users\jaywi\Downloads\SW Dev\Heroku Node JS\CSQ\node_modules\unirest\node_modules\form-data\lib\form_data.js:105:13 at FSReqCallback.oncomplete (node:fs:198:21) { code: 'ERR_HTTP_INVALID_HEADER_VALUE' }