4

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.

sawa
  • 165,429
  • 45
  • 277
  • 381
thinkfuture
  • 241
  • 1
  • 5
  • 12

1 Answers1

5

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.... 
...
Andrea Campolonghi
  • 546
  • 2
  • 5
  • 15
  • 1
    Example: `RestClient.post "http://#{udid}:#{SECRET_KEY}@#{API_BASE_PATH}/trip", {:from => from, :to => to}, {:user_agent => user_agent}` – samvermette May 04 '13 at 07:27