我正在尝试使用 Ruby on Rails 与 Salesforce API 进行通信。我可以很容易地获取数据,但是我在将数据发布到服务器时遇到了问题。根据 Quinton Wall 的帖子,我正在使用 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' 的媒体类型", "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 格式发布,以便销售人员不会拒绝它?
任何帮助,将不胜感激。干杯
最佳答案
Content-Type header 需要设置为“application/json”。这可以通过插入 :headers => {'Content-Type' => 'application/json'} 作为要发布的参数来完成,即:
response = post(Accounts.root_url+"/sobjects/Account/",
:body => {:name => "graham"}.to_json,
:headers => {'Content-Type' => 'application/json'} )
关于ruby-on-rails - 使用 HTTParty 将 Content-Type 更改为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6154176/