function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jeffdonthemic2jeffdonthemic2 

UNSUPPORTED_MEDIA_TYPE with Ruby and REST API using POST

I started out with Quinton's article here and am trying to build out some samples using Rails and the REST API. I can query for and fetch records but am having problems creating new records.

 

Here's my code:

 

 

  def self.create()
    Accounts.set_headers
    options = {
      :body => {
          :Name => "TestRailsApp"
      }
    }
    response = post(Accounts.root_url+"/sobjects/Account/", options)
    puts response.body, response.code, response.message
  end

 

 

But I am getting the following response back:

 

 

opening connection to na5.salesforce.com...
opened
<- "POST /services/data/v21.0/sobjects/Account/ HTTP/1.1\r\nAuthorization: OAuth 00D700000009Bki!ARQAQHsUMNBjZTLupOGjYhGDQ9k8SN0FC9qGnVMhpM5rqgLsOBPVhgtsgwqYzELkcX47FlyqQhGAf83eaXXXXX\r\nConnection: close\r\nHost: na5.salesforce.com\r\nContent-Length: 8\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n"
<- "Name=TestRailsApp"
-> "HTTP/1.1 415 Unsupported Media Type\r\n"
-> "Server: \r\n"
-> "Content-Type: application/json; charset=UTF-8\r\n"
-> "Content-Length: 135\r\n"
-> "Date: Tue, 26 Apr 2011 14:15:34 GMT\r\n"
-> "Connection: close\r\n"
-> "\r\n"
reading 135 bytes...
-> "[{\"message\":\"MediaType of 'application/x-www-form-urlencoded' is not supported by this resource\",\"errorCode\":\"UNSUPPORTED_MEDIA_TYPE\"}]"
read 135 bytes
Conn close
[{"message":"MediaType of 'application/x-www-form-urlencoded' is not supported by this resource","errorCode":"UNSUPPORTED_MEDIA_TYPE"}]
415
Unsupported Media Type

 

 

Any ideas? Thanks!

 

Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com

http://www.cloudspokes.com

Best Answer chosen by Admin (Salesforce Developers) 
jeffdonthemic2jeffdonthemic2

That did the trick Simon!! Thanks for the help. Here' s my solution:

 

 

  def self.create()
    Accounts.set_headers
    headers 'Content-Type' => "application/json"
    options = {
      :body => {
          :Name => "Test1234"
      }.to_json
    }
    response = post(Accounts.root_url+"/sobjects/Account/", options)
    puts response.body, response.code, response.message
  end

 

 

 

Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com

http://www.cloudspokes.com

All Answers

SuperfellSuperfell

You're sending an HTML form as the post payload, it needs to be an xml or json document instead.

jeffdonthemic2jeffdonthemic2

That did the trick Simon!! Thanks for the help. Here' s my solution:

 

 

  def self.create()
    Accounts.set_headers
    headers 'Content-Type' => "application/json"
    options = {
      :body => {
          :Name => "Test1234"
      }.to_json
    }
    response = post(Accounts.root_url+"/sobjects/Account/", options)
    puts response.body, response.code, response.message
  end

 

 

 

Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com

http://www.cloudspokes.com

This was selected as the best answer