Ruby Net::HTTP - 停止自动转义引号?
我有以下代码:
http = Net::HTTP.new("www.something.com", 80)
http.set_debug_output($stdout)
http.post("/blah", "something", {'random-parameter' => 'value1="something",value2="somethingelse"'})
然后,当我从 stdout 读取输出时,它看起来像这样:
<- "POST /blah HTTP/1.1\r\nAccept: */*\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: close\r\nrandom-parameter: value1=\"something\",value2=\"somethingelse\"\r\nContent-Length: 9\r\nHost: www.something.com\r\n\r\n"
<- "something"
引号被转义。问题是斜杠似乎被发送到服务器,而服务器不喜欢这样。我收到一条错误消息,内容是
Unknown value for random-parameter in header: {value1=\\\"something\\\"value2=\\\"somethingelse\\\"}
“所以我的问题是,是否有办法告诉 Net::HTTP 不要插入这些斜杠,或者在发送标头之前将它们删除?
” 澄清:
我使用 Ruby 1.8.7 和 Rails 2.0.2。
我认为 Rails 可能正在转义字符,但我不知道如何让它停止。
I have the following code:
http = Net::HTTP.new("www.something.com", 80)
http.set_debug_output($stdout)
http.post("/blah", "something", {'random-parameter' => 'value1="something",value2="somethingelse"'})
Then when I read the output from stdout, it looks like this:
<- "POST /blah HTTP/1.1\r\nAccept: */*\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: close\r\nrandom-parameter: value1=\"something\",value2=\"somethingelse\"\r\nContent-Length: 9\r\nHost: www.something.com\r\n\r\n"
<- "something"
with the quotes being escaped. The problem is that the slashes seem to be sent to the server, which doesn't like that. I'm getting an error that says
Unknown value for random-parameter in header: {value1=\\\"something\\\"value2=\\\"somethingelse\\\"}
So my question is, is there a way to tell Net::HTTP to not insert those slashes, or strip them out before sending the header?
Clarifications:
I'm using Ruby 1.8.7 with Rails 2.0.2.
I think it may be Rails that is escaping the characters, but I'm not sure how to make it stop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定正确构建了标头吗? Net::HTTP 在发送请求时不引用引号。您可以使用例如 netcat (nc) 轻松验证它:
Terminal 1:
Terminal 2 (in irb):
Result (in Terminal 1):
我认为您实际上可能想要做什么(不确定,但我猜测)更符合 HTTP 规范:
Rails 可能会将您的第一个 value1=... 解释为整个值..当您可能需要用 ';' 而不是 ',' 分隔值时。
另请注意,您通常不会通过请求标头传递参数。但也许这就是您在这种情况下想要做的(?)否则您应该在 URL 本身中传递参数,例如 param1=foo¶m2=bar,或者使用 x-www-form-urlencoded 来传递参数。
请参阅此处的备忘单:
http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
Are you sure you're building up the header correctly? Net::HTTP does not quote quotes when sending the request. You can easily verify it by using for example netcat (nc):
Terminal 1:
Terminal 2 (in irb):
Result (in terminal 1):
I think what you actually may want to do (not sure, but I'm guessing) is something more along the lines of the HTTP spec:
Rails is probably interpreting your first value1=... as the whole value..when you probably need to separate the values with ';', not ','.
Also note that you don't usually pass parameters through request headers. But maybe that's what you want to do in this case(?) Otherwise you should either pass the parameters in the URL itself like param1=foo¶m2=bar, or use x-www-form-urlencoded to pass the parameters.
See here for a cheat sheet:
http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
问题不是您的名称/值对是“随机参数”的值,因此需要对它们进行转义。
我希望当您检查控制器方法中的随机参数值时,它不会有斜杠 - 您可以调试代码或打印参数以查看收到的内容吗?
Isn't the problem that your name/value pairs are the value for "random-parameter", hence they need to be escaped.
I'd expect that when you inspect the random-parameter value in the controller method that it would not have the slashes - can you debug the code or print the parameter to see whats being received?