Rest-Client Ruby Gem 标头

发布于 2024-11-06 03:47:24 字数 1001 浏览 1 评论 0原文

我正在尝试使用rest-client gem 来发布某些内容,但由于某种原因,我不断收到内部服务器错误。我在 Chrome 上使用了 Simple REST Client,并且得到了相同的错误,除非我发送以下标头:

Content-Type: application/x-www-form-urlencoded

因此,我尝试通过 post 请求发送该标头,但由于某种原因,它仍然无法正常工作。这是我尝试过的:

RestClient.post "server", :content_type=>"Content-Type: application/x-www-form-urlencoded",:name=> 'Test', :message_type=> 'Request', :version=> '2.0'
RestClient.post "server", {:content_type=> "Content-Type: application/x-www-form-urlencoded"},:name=> 'Test', :message_type=> 'Request', :version=> '2.0'
RestClient.post "server", {"Content-Type" =>"Content-Type: application/x-www-form-urlencoded"},:name=> 'Test', :message_type=> 'Request', :version=> '2.0'
RestClient.post "server", :header => {:content_type=>: "Content-Type: application/x-www-form-urlencoded"},:name=> 'Test', :message_type=> 'Request', :version=> '2.0'

有人可以告诉我我做错了什么吗?已经搜索了一些指示如何设置标题的文档,但似乎没有任何效果。

I'm attempting to use the rest-client gem to post something, but for some reason, I keep getting Internal Server Error. I used Simple REST Client on Chrome, and got the same error unless I sent the following header:

Content-Type: application/x-www-form-urlencoded

So I'm trying to send that header with the post request, but for some reason, it's still not working. Here is what I tried:

RestClient.post "server", :content_type=>"Content-Type: application/x-www-form-urlencoded",:name=> 'Test', :message_type=> 'Request', :version=> '2.0'
RestClient.post "server", {:content_type=> "Content-Type: application/x-www-form-urlencoded"},:name=> 'Test', :message_type=> 'Request', :version=> '2.0'
RestClient.post "server", {"Content-Type" =>"Content-Type: application/x-www-form-urlencoded"},:name=> 'Test', :message_type=> 'Request', :version=> '2.0'
RestClient.post "server", :header => {:content_type=>: "Content-Type: application/x-www-form-urlencoded"},:name=> 'Test', :message_type=> 'Request', :version=> '2.0'

Can someone tell me what I'm doing wrong? Have searched all over for some docs which indicate how to set header, but nothing seems to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

绝情姑娘 2024-11-13 03:47:24

我尝试了这样的方法并且工作得很好:

options[:multipart] = true
# more options

resource = RestClient::Resource.new uri, options[:username], options[:password]
resource.post options do |response, request, result|

..............

end

看起来必须像使用普通参数一样传递多部分。第二个参数将简单地添加到标题中。

希望这有帮助

resource.post {params and request}, {custom headers here} do.... 
...

I tried something like this and worked fine:

options[:multipart] = true
# more options

resource = RestClient::Resource.new uri, options[:username], options[:password]
resource.post options do |response, request, result|

..............

end

Looks like multipart must be passed as you do with normal parameters. The second arguments will be simply added to the headers.

Hope this helps

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