POST 包含 ascii 字符的请求 [x-www-form-urlencoded]

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

我已经阅读了很多问题,但 none 是我的问题的解决方案。

我正在尝试提交页面字符集为 ISO-8859-1 的表单。我正在使用 github.com/request/request.

根据文档,要提交表单,我们基本上需要:

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

我试过了,但是ascii字符不对。 该页面不接受我发送的内容:

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

request.post(options, function...)

我也试试:

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...)

编辑:

我也尝试使用 charset=Windows-1252 和 Iconv:var iconv = new Iconv('UTF-8', 'ISO-8859-1');

问题出在查询字符串上:

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.

所以我使用 Node 中的 querystring.stringify(obj[, sep][, eq][, options]),最终结果是:

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.