如何解决使用 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-client 没有传递所有标头: {:content_type => "application/json",: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
有什么建议吗? 谢谢
解决方法
rest-client
将在多种情况下返回 404 错误,而不仅仅是未找到错误
这里最好的方法是返回并检查服务器的响应
>> 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
来检查错误
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。