我有其余的客户端 gem,我正在定义这样的请求:
url = 'http://someurl'
request = {"data" => data}.to_json
response = RestClient.post(url,request,:content_type => :json, :accept => :json)
但是我需要将 HTTP header 设置为某些内容。例如 API key 。这可以在 curl 中完成:
curl -XHEAD -H x-auth-user: myusername -H x-auth-key: mykey "url"
在 ruby 中执行此操作的最佳方法是什么?使用这个 gem ?或者我可以手动执行此操作以获得更多控制权。
最佳答案
The third parameter is the headers hash .
你可以通过以下方式做你想做的事:
response = RestClient.post(
url,
request,
:content_type => :json, :accept => :json, :'x-auth-key' => "mykey")
关于ruby - 在 Ruby 中设置请求 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12161640/