Ruby Net::HTTP - 停止自动转义引号?

发布于 2024-11-16 23:36:55 字数 902 浏览 3 评论 0原文

我有以下代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪菊怪哟 2024-11-23 23:36:55

您确定正确构建了标头吗? Net::HTTP 在发送请求时不引用引号。您可以使用例如 netcat (nc) 轻松验证它:

Terminal 1:

> nc -v -l -p 2323

Terminal 2 (in irb):

> http = Net::HTTP.new("localhost", 2323)
> http.post("/blah", "something", {'random-parameter' => ... )

Result (in Terminal 1):

listening on [any] 2323 ...
connect to [127.0.0.1] from localhost [127.0.0.1] 37598
POST /blah HTTP/1.1
Connection: close
Accept: */*
Random-Parameter: value1="something",value2="somethingelse"
Content-Type: application/x-www-form-urlencoded
Content-Length: 9
Host: localhost:2323

something

我认为您实际上可能想要做什么(不确定,但我猜测)更符合 HTTP 规范:

> http.post("/blah", "something", {
    'random-parameter' => 'value1="something"; value2="somethingelse"' })

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:

> nc -v -l -p 2323

Terminal 2 (in irb):

> http = Net::HTTP.new("localhost", 2323)
> http.post("/blah", "something", {'random-parameter' => ... )

Result (in terminal 1):

listening on [any] 2323 ...
connect to [127.0.0.1] from localhost [127.0.0.1] 37598
POST /blah HTTP/1.1
Connection: close
Accept: */*
Random-Parameter: value1="something",value2="somethingelse"
Content-Type: application/x-www-form-urlencoded
Content-Length: 9
Host: localhost:2323

something

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:

> http.post("/blah", "something", {
    'random-parameter' => 'value1="something"; value2="somethingelse"' })

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

无风消散 2024-11-23 23:36:55

问题不是您的名称/值对是“随机参数”的值,因此需要对它们进行转义。

我希望当您检查控制器方法中的随机参数值时,它不会有斜杠 - 您可以调试代码或打印参数以查看收到的内容吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文