简体   繁体   中英

POST request containing ascii characters [x-www-form-urlencoded]

I already read a lot of questions but none is solution for my problem.

I'm trying to submit a form where the charset of the page is ISO-8859-1. I'm using github.com/request/request .

From documentation, to submit a form we basically need to:

request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ })

I try it but the ascii characters wasn't right. The page doesn't accept what I'm sending:

var options = { 
  url: formData.action,
  encoding: 'ascii',
  form: { username: 'teste', password: '23dçkasã' }
}

request.post(options, function...)

I try also:

var options = { 
  url: formData.action,
  headers: { 
    'Content-Type': 'application/x-www-form-urlencoded; charset=iso-8859-1'
  },
  form: { username: 'teste', password: '23dçkasã' }
}

request.post(options, function...)

EDIT:

I try also with charset=Windows-1252 and with Iconv: var iconv = new Iconv('UTF-8', 'ISO-8859-1');

The problem was on the query string:

The password that I use for testing was djaçf3 and the query string made by the POST request was dja%C3%A7f3 but with ISO-8859-1 it should be dja%E7f3 .

So I use querystring.stringify(obj[, sep][, eq][, options]) from Node and the final result is:

var options = { 
  url: formData.action,
  form: querystring.stringify({ username: 'xpto', password: 'djaçf3' }, null, null, { encodeURIComponent: escape })
}

request.post(options, function...)

Options object may contain encodeURIComponent property (querystring.escape by default), it can be used to encode string with non-utf8 encoding if necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM