[英]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. 我正在尝试提交一个表格,其中页面的字符集是ISO-8859-1。 I'm using github.com/request/request .
我正在使用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. 我试了但是ascii字符不对。 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');
我也尝试使用
charset=Windows-1252
和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 wasdja%C3%A7f3
but with ISO-8859-1 it should bedja%E7f3
.我用于测试的密码是
djaçf3
,POST请求djaçf3
的查询字符串是djaçf3
dja%C3%A7f3
但是ISO-8859-1应该是dja%E7f3
。
So I use querystring.stringify(obj[, sep][, eq][, options]) from Node and the final result is: 所以我使用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.
Options对象可能包含encodeURIComponent属性(默认为querystring.escape),如果需要,它可用于对非utf8编码的字符串进行编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.