简体   繁体   English

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

[英]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 was dja%C3%A7f3 but with ISO-8859-1 it should be dja%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.

相关问题 如何使发布请求具有bas ic auth和内容类型x-www-form-urlencoded - how to make post request baswith ic auth and content type x-www-form-urlencoded 如何使用“内容类型”:“ application / x-www-form-urlencoded”发出发布请求? - How to make a Post request with “content type”: “application/x-www-form-urlencoded”? 使用application / x-www-form-urlencoded使用node.js在发布请求中发送数组 - Send Array in post request using node.js using application/x-www-form-urlencoded node-fetch 发送 post 请求,正文为 x-www-form-urlencoded - node-fetch send post request with body as x-www-form-urlencoded 邮递员x-www-form-urlencoded会为POST请求返回空值,即使输入字段已填写 - Postman x-www-form-urlencoded returns null value for a POST request even though the input field is filled 具有 application/x-www-form-urlencoded 格式的 Node.js Axios POST 请求? - Node.js Axios POST request with application/x-www-form-urlencoded format? 使用 axios 发出 x-www-form-urlencoded 请求 - Making a x-www-form-urlencoded request with axios Axios post form urlencoded 请求 application/x-www-form-urlencoded - Axios post form urlencoded requests application/x-www-form-urlencoded 使用“Content-Type”:“application/x-www-form-urlencoded”从 axios 发送帖子请求会给出 401 Unauthorized 响应 - Sending post request from axios with "Content-Type" : "application/x-www-form-urlencoded" gives an 401 Unauthorized response 如何使用同构提取使用application / x-www-form-urlencoded标头和URLSearchParams进行POST - How to POST with application/x-www-form-urlencoded header and URLSearchParams using isomorphic-fetch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM