Nick Urban Nick Urban - 1 year ago 93
Ruby Question

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

For instance, in the RestClient console: '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'})

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 Source

You might want to put json as string as your payload instead of hash. For example, do: '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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download