使用curl发出请求,相当于REST_CLIENT请求



我在Curl 中使用以下请求

curl -X GET -k -H 'Content-Type: application/json' -H 'Authorization: Bearer XXXXXXX' -H 'RowId: 100' -i 'https://url.xxx/row'

以及使用REST_CLIENT(2.1.0(的请求:

RestClient::Request.execute(:url => "https://url.xxx/row", :method => :get, :verify_ssl => false, :headers => {:content_type => "application/json", :Authorization => "Bearer XXXXXXX", :RowId => 100})

RestClient::NotFound:404未找到

第一个请求(Curl(正在工作,但RestClient中的等效请求不工作。

问题是rest客户端没有传递所有标头:{:content_type=>"application/json",:Authorization=>"Bearer XXXXXXX",:RowId=>100}

只使用content_type和Authorization,当请求发送时,不使用其他

net/http:也有类似的问题

require 'net/http'
uri = URI('https://url.xxx/row')
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
https.ssl_version = :TLSv1
req = Net::HTTP::Get.new uri
req['content_type'] = 'application/json'
req['Authorization'] = "Bearer #{token}"
req['RowId'] = 100
res = https.request req
puts res.body

有什么建议吗?Thx

rest-client将在多种情况下返回404错误,而不仅仅是针对未找到的错误

这里最好的方法是返回并检查服务器的响应

根据他们的文件:https://github.com/rest-client/rest-client#exceptions-参见http:wwww3orgprotocolsrfc2616rfc2616-sec10html

>> RestClient.get 'http://example.com/nonexistent'
Exception: RestClient::NotFound: 404 Not Found
>> begin
RestClient.get 'http://example.com/nonexistent'
rescue RestClient::ExceptionWithResponse => e
e.response
end
=> <RestClient::Response 404 "<!doctype h...">

可能还会尝试调用e.response.body来检查错误

最新更新