ruby-on-rails - 请求 header 中的 HTTParty 整数

标签 ruby-on-rails ruby httparty net-http

我需要 POST 到 API,该 API 需要请求中包含一个名为“Timestamp”的 header ,其值必须是整数(从纪元开始的当前时间为整数)。

我尝试使用 HTTPartyNet::HTTP 执行此操作,如下所示:

response = HTTParty.post(route, body: options[:body].to_json,headers: { 'Timestamp' => Time.now.to_i, 'Authorization' => "Bearer #{options[:token]}",'Content-Type' => 'application/json' })
# >> NoMethodError: undefined method `strip' for 1522273989:Integer

它对 header 值调用 strip,这会引发错误,因为您无法对整数调用 strip

有人知道如何在请求 header 中传递整数吗?

最佳答案

对整数调用.to_s:

response = HTTParty.post(route, body: options[:body].to_json,headers: { 'Timestamp' => Time.now.to_i.to_s, 'Authorization' => "Bearer #{options[:token]}",'Content-Type' => 'application/json' })

您实际上无法在 header 中发送整数 - 接收端的服务器必须转换请求中的明文 header 。

Header fields are colon-separated name-value pairs in clear-text string format, terminated by a carriage return (CR) and line feed (LF) character sequence.
List of HTTP header fields - Wikipedia

关于ruby-on-rails - 请求 header 中的 HTTParty 整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49544792/

相关文章:

ruby-on-rails - 回形针不会在 Rails 应用程序中保存图像

ruby-on-rails - 在 Active Record 中返回 sibling (child.parent.children)

mysql - 在 Rails 中使用 ActiveRecord 实现复杂关系

ruby-on-rails - rails : allow download of files stored on S3 without showing the actual S3 URL to user

ruby-on-rails - 在带有 HTTPParty 的 Controller 中解析 JSON

ruby - 使用 Ruby 取消转义字符串中的字符

ruby - 如何使用 httparty 模仿 curl Action

javascript - JQuery:在链接之前执行帖子

ruby - 复杂的graphviz树结构

ruby-on-rails - 是否有可能以动态时间间隔运行 cron 作业?请建议