使用 HTTParty 将 Content-Type 更改为 JSON
我正在尝试使用 Ruby on Rails 与 Salesforce API 进行通信。我可以轻松获取数据,但在将数据发布到服务器时遇到问题。我按照 Quinton Wall 的帖子使用 HTTParty:
但我似乎能够从 salesforce 服务器得到的只是我将正文提交为 html
{"message"=>"MediaType of '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 形式发布这样销售人员就不会拒绝它?
任何帮助将不胜感激。干杯
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Content-Type 标头需要设置为“application/json”。这可以通过插入 :headers => 来完成{'内容类型' => 'application/json'} 作为 post 的参数,即:
The Content-Type header needs to be set to "application/json". This can be done by inserting :headers => {'Content-Type' => 'application/json'} as a parameter to post, ie:
您必须将 Content-Type 标头设置为 application/json。我没有使用过 HTTParty,但看起来你必须做一些事情,比如
我有点惊讶格式选项不会自动执行此操作。
You have to set the Content-Type header to application/json. I haven't used HTTParty, but it looks like you have to do something like
I'm somewhat surpised that the format option doesn't do this automatically.