How do I make Ruby's RestClient gem respect content_type on post?

允我心安 提交于 2019-11-30 01:27:19

问题


For instance, in the RestClient console:

RestClient.post 'http://localhost:5001', {:a => 'b'}, :content_type => 'application/json'

This does not send application/json as the content type. Instead I see:

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

I was able to trace the change to restclient/payload.rb:

  class UrlEncoded < Base
  ...

  def headers
    super.merge({'Content-Type' => 'application/x-www-form-urlencoded'})
  end
end

Replacing super.merge with super causes the content type to be respected, but obviously that's not a real solution. Does anyone know the proper way to fix this? Thanks.


回答1:


You might want to put json as string as your payload instead of hash. For example, do:

RestClient.post 'http://localhost:5001','{"a":"b"}',:content_type => 'application/json'

If you look at the payload.rb, it shows that it will use the Base clase instead of UrlEncoded class if the payload is string. Try that and see if that work for you.




回答2:


Fact:

For :post request, when payload is a Hash, the Content-Type header will be always overridden to application/x-www-form-urlencoded.

Reproduciable with rest-client (2.0.0).

Solution:

Convert the hash payload to json string.

require 'json'

payload.to_json

There is a ticket in rest-client's repo:




回答3:


I'd like to add that my issue was when using RestClient::Request.execute (as opposed to RestClient.post or RestClient.get).

The problem was with how I was setting :content_type and :accept. From the examples I saw it felt like they should be top level options like this:

res = RestClient::Request.execute(
  :method => :get,
  :url => url,
  :verify_ssl =>  false,
  :content_type => :json,
  :accept => :json,
  :headers => { 
    :Authorization => "Bearer #{token}", 
  },
  :payload => '{"a":"b"}'
)

But you actually have to put them within :headers like this:

res = RestClient::Request.execute(
  :method => :get,
  :url => url,
  :verify_ssl =>  false,
  :headers => { 
    :Authorization => "Bearer #{token}", 
    :content_type => :json,
    :accept => :json
  },
  :payload => '{"a":"b"}'
)



回答4:


I was trying to submit username and password, alongside csrf tokens and auth cookie, via POST in form data format. Payload conversion to json and explicitly setting content-type header did not help. I ended up passing payload as a query string and removed its conversion to JSON:

RestClient::Request.execute(
  method: :post, 
  url: 'http://app_url/login.do',
  payload: "username=username&password=password&_csrf=token",
  headers: {'X-XSRF-TOKEN' => 'token'},
  cookies: {'XSRF-TOKEN' =>  cookie_object}
)

Another option would also be to use encode_www_form, but a query string works better for my specific use case.

While this is not a common case and all depends on parameter format expected by the back end, it's still a viable option to pass a query string in the POST body if the server expects url encoding as POST body. Hopefully this might help someone.



来源:https://stackoverflow.com/questions/8189932/how-do-i-make-rubys-restclient-gem-respect-content-type-on-post

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!