Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

I am trying to use Ruby on Rails to communicate with the Salesforce API. I can fetch data easily enough but I am having problems posting data to the server. I am using HTTParty as per Quinton Wall's post here:

https://github.com/quintonwall/omniauth-rails3-forcedotcom/wiki/Build-Mobile-Apps-in-the-Cloud-with-Omniauth,-Httparty-and-Force.com

but all I seem to be able to get from the salesforce server is the error that I am submitting the body as html

{"message"=>"MediaType of 'application/x-www-form-urlencoded' is not supported by this resource", "errorCode"=>"UNSUPPORTED_MEDIA_TYPE"}

the responsible code looks like:

require 'rubygems'

require 'httparty'

class Accounts

  include HTTParty

  format :json

  ...[set headers and root_url etc]

  def self.save

    Accounts.set_headers

    response = (post(Accounts.root_url+"/sobjects/Account/", :body => {:name => "graham"}.to_json))

  end

end

anyone have an idea why the body should be being posted as html and how to change this so that it definitely goes as json so that salesforce doesn't reject it?

Any help would be appreciated. cheers

1 Answer

0 votes
by (32.1k points)

Try setting the Content-Type header to application/JSON. I didn't use HTTParty, but you can use something like this:

response = (post(Accounts.root_url+"/sobjects/Account/", :body => {:name => "graham"}.to_json) , :options => { :headers => { 'Content-Type' => 'application/json' } } )

Note: The format doesn't do this automatically.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...