thinkfuture
thinkfuture

Reputation: 241

Rest-Client Ruby Gem Headers

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.

Upvotes: 4

Views: 6415

Answers (1)

Andrea Campolonghi
Andrea Campolonghi

Reputation: 580

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.... 
...

Upvotes: 5

Related Questions