您好我能成功发布Post man,但无法从Ruby Rest客户端发帖。
发帖人请求
POST /endpoint HTTP/1.1
Host: host:11400
Accept: application/json
HTTP_USER: userid
fname: fname
lname: lname
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 589a5345-e384-bd71-d690-60987165487b
{ "rtdt":"09/08/2016","jobs":[{"pid":53} , {"pid":54}]}
其余的代码我试过多种方式,包括下面的
尝试了方法
p RestClient.post 'http://url_details',
Accept: 'application/json',
'HTTP_USER'.to_sym => 'userid',
fname: 'name',
lname: '',
'Content-Type'.to_sym => 'application/json',
payload: JSON.parse('{ "rtdt":"09/08/2016","jobs":[{"pid":53} , {"pid":54}]}')
尝试了方法
p RestClient.post 'http://url_details/job', http_user: 'userid', content_type: :json, accept: :json
尝试了方法
p @uber_ride = (RestClient::Request.execute(
:method => :post,
:url => 'http://url_details/job',
'HTTP_USER' => 'userid',
:headers => {:content_type => 'application/json', :accept => 'application/json', :HTTP_USER => 'userid', :fname => 'name', :lname => 'lname'}
))
尝试了方法
p '++++++++++++++++++++++++++++++++'
p RestClient.post($Current_Api_Endpoint,
$Current_Payload,
:http_user =>'userid',
headers:
{:accept => 'application/json',
:content_type => 'application/json',
:http_user =>'userid',
:fname => 'name',
:lname => 'lname'}
)
p '++++++++++++++++++++++++++++++++'
尝试了方法
p env1 = ENV['http_proxy']
# p a = {:method => :post, :url => 'http://url_details/job', :headers => { :accept => 'application/json', :content_type => 'application/json', :http_user => 'userid', :fname => 'name', :lname => 'lname' }, :payload => { :rtdt => "06/10/2016",:jobs => [{:pid => 53} , {:pid => 54}]} }
p a = {
method: 'post',
url: 'http://url_details/job',
proxy: ENV['http_proxy'],
headers: {accept: 'application/json', content_type: 'application/json', http_user: 'userid', fname: 'name', lname: 'lname'},
payload: { rtdt: '06/10/2016', jobs: {pid: 53}}
}
我收到错误 HTTP状态500 - 在请求中找不到HTTP_USER标头 torg.springframework.security.web.authentication
从那以后我发现了这个问题。除了这一个之外,所有标题都可以发送 - 这个标题在CAPS中。其余的人都是小角色。如何解决
答案 0 :(得分:1)
Ruby的Net::HTTP
实现不会传输ALL_CAPS_UNDERSCORED标头名称。 RestClient
依赖于Net::HTTP
。因此,例如,HTTP_USER
标头在传输时实际上会转换为Http_user
。
警示:限制
但是,Curl没有这个限制。它逐字传输标题名称。因此,您可以使用curb gem,而不是使用RestClient
,它是libcurl的包装器。这似乎有效:
require "curb"
Curl::Easy.http_post("http://URL_REDACTED") do |http|
http.headers["Accept"] = "application/json"
http.headers["Content-Type"] = "application/json"
http.headers["HTTP_USER"] = "userid"
http.post_body = '{ "rtdt":"09/08/2016","jobs":[{"pid":53} , {"pid":54}]}'
end
替代方案:excon
excon宝石是另一种可能性。它在纯Ruby中实现HTTP,而不依赖于Net::HTTP
。它执行自己的头处理并允许ALL_CAPS头。所以这应该有效:
require "excon"
Excon.post(
"http://URL_REDACTED",
:headers => {
"Accept" => "application/json",
"Content-Type" => "application/json",
"HTTP_USER" => "userid"
},
:body => '{ "rtdt":"09/08/2016","jobs":[{"pid":53} , {"pid":54}]}'
)
无论如何,我认为你尝试连接的服务器最终在这里有问题,因为标题应该被评估为不区分大小写:Are HTTP headers case-sensitive?。如果服务器要求标题全部为大写,那就是错误。
答案 1 :(得分:0)
RestClient README examples表示正确的用法是:
RestClient.post(url, payload, headers)
所以你要做的就是:
RestClient.post(
"http://URL_REDACTED",
'{ "rtdt":"09/08/2016","jobs":[{"pid":53} , {"pid":54}]}',
content_type: :json,
accept: :json,
"HTTP_USER" => "userid"
)
我针对http://requestb.in进行了测试,得到了预期的标题和正文:
HEADERS
Total-Route-Time: 0
Connection: close
Via: 1.1 vegur
Connect-Time: 0
Http-User: userid
Content-Type: application/json
Content-Length: 55
User-Agent: rest-client/2.0.0 (darwin15.4.0 x86_64) ruby/2.3.1p112
X-Request-Id: c7cd819f-8901-41b9-8cbb-3b6d1895cd67
Accept-Encoding: gzip
Host: requestb.in
Accept: application/json
RAW BODY
{ "rtdt":"09/08/2016","jobs":[{"pid":53} , {"pid":54}]}