使用HTTParty将Content-Type更改为JSON

我正在尝试使用Ruby on Rails与Salesforce API进行通信。 我可以轻松地获取数据,但我在将数据发布到服务器时遇到问题。 我按照Quinton Wall的post使用HTTParty:

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

但我似乎能够从salesforce服务器获得的是我以html身份提交正文的错误

{“message”=>“此资源不支持’application / x-www-form-urlencoded’的MediaType”,“errorCode”=>“UNSUPPORTED_MEDIA_TYPE”}

负责的代码如下:

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 

任何人都知道为什么身体应该被发布为html以及如何更改它以便它肯定像json一样,以便salesforce不拒绝它?

任何帮助,将不胜感激。 干杯

您必须将Content-Type标头设置为application / json。 我没有使用过HTTParty,但看起来你必须做类似的事情

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

我有点鄙视格式选项不会自动执行此操作。

Content-Type标头需要设置为“application / json”。 这可以通过插入:headers => {‘Content-Type’=>’application / json’}作为要发布的参数来完成,即:

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