LucasSeveryn
LucasSeveryn

Reputation: 6272

How to encode form data passed in the post request

I can't switch to AJAX because it's a third party server. I'm using nodejs and request libraries. The difference in the raw data between a working (client machine test) and non-working (node js machine) request seems to be the way spaces are encoded. On the client machine, in the chrome inspector I see spaces as + signs. That request works. However in nodejs terminal, I see spaces as %20. If I try adding + myself I will see them as %2B.

The request results in an automatic download of a csv file that I want to save to the server filesystem.

     fd={'key A to B':'value',
     'key B to C':'value B to C',
     'key C' : 'valueC',
     'numKey' : 1} 

    request.post('http://service.com/upload').form(fd).pipe(fs.createWriteStream('./myfile.csv'))

Upvotes: 1

Views: 843

Answers (1)

LucasSeveryn
LucasSeveryn

Reputation: 6272

Solved it by moving away from the request.post and using a more general request function:

request({
   method: 'POST',
   uri: 'http://example.com/',
   headers: { 
     host: 'example.com',
     'content-type': 'application/x-www-form-urlencoded' },
     body: $.param(fd)}
).pipe(fs.createWriteStream('myfile.csv'))

Upvotes: 1

Related Questions