Nick Urban Nick Urban - 6 months ago 10
Ruby Question

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

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.

Answer

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.

Comments