How to add headers to a httparty call

烂漫一生 提交于 2020-01-06 21:05:53

问题


Im trying to add headers to my api call

Heres what i have tried at the moment

api1 = HTTParty.get(URI.encode('apiurllink' + tmname + '&date=' + fromdate + ' TO ' + todate + '', :headers => {"Authorization" => "Bearer apikey"})).parsed_response

This is returning this error

TypeError: no implicit conversion of Hash into String

How do I fix this?


回答1:


Your attempt is raising a TypeError since you are passing the :headers => {"Authorization" => "Bearer apikey"} hash to URI.encode and not HTTParty.get.

api1 = HTTParty.get(URI.encode('apiurllink' + tmname + '&date=' + fromdate + ' TO ' + todate + ''), :headers => {"Authorization" => "Bearer apikey"}).parsed_response

A better way is to use the query option and get HTTParty to construct the query string for you.

response = HTTParty.get('/someuri', 
  query: {
    date: "#{fromdate} TO #{todate}",
    foo: "bar"
  },
  headers: {
    "Authorization" => "Bearer apikey"
  }
)


来源:https://stackoverflow.com/questions/35924858/how-to-add-headers-to-a-httparty-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!