这是最奇怪的事情。当我添加;
in set_form_data
, value
被解释为value;
在服务器端。当我删除;
,“dontescape”的值被解释为file%3a%2f%2f%2fpath%2fto
。到底发生了什么?我不希望任何东西被转义,除非我明确地调用 CGI::escape!请帮忙 :)
postParams = {
'key1' => 'value',
'dontescape' => 'file:///path/to'
}
url = URI.parse('https://my.url')
req = Net::HTTP::Post.new(url.path)
req.basic_auth('username', 'password')
req.set_form_data(postParams, ';')
sock = Net::HTTP.new(url.host, 443)
sock.use_ssl = true
sock.ssl_version = 'SSLv3'
sock.start do |http|
response = http.request(req) do
return response.body
end
发生这种情况是不好的:PHP、Java、Node —— 它们都不会这样做。
首先,我重写:
# Module to override
module Net
module HTTPHeader
def postUrlBuilder(postParams)
@queryUrl = ''
if (postParams.nil? or postParams == 0)
# Null or empty item
else
count = 0
postParams.each_pair do |key,value|
if (count == 0)
@queryUrl = @queryUrl + key + '=' + value
count = count + 1
else
@queryUrl = @queryUrl + '&' + key + '=' + value
end
end
end
return @queryUrl
end
def set_form_data(postParams, sep = '&')
self.body = postUrlBuilder(postParams)
self.content_type = 'application/x-www-form-urlencoded'
end
alias form_data= set_form_data
end
end
然后我写了自己的post方法:
def monkey_patch_post_request(postParams)
url = URI.parse(@monkeyPatchUrl)
req = Net::HTTP::Post.new(url.path)
req.basic_auth @companyId, @apiKey
req.set_form_data(postParams, sep = '&')
sock = Net::HTTP.new(url.host, 443)
sock.use_ssl = true
sock.ssl_version='SSLv3'
sock.start do |http|
response = http.request(req)
return response.body
end
end
这应该有帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)