首页 > 解决方案 > 使用 HTTParty 将时间转换为可消化的 JSON 字符串

问题描述

以下字符串返回结果

@result = HTTParty.post(
  'https://test.co.uk/search', 
  :body => '{ "requestAuditInfo": {  "agentCode": "MLG001",  
  "requestDateTime": "2018-07-29T07:03:38Z" [...]

然而

@result = HTTParty.post(
  'https://test.co.uk/search', 
  :body => '{ "requestAuditInfo": {  "agentCode": "MLG001",  
  "requestDateTime": Time.now.utc.iso8601 [...]

返回零

Time.now.utc.iso8601返回 控制台,"2018-07-29T07:03:38Z"这是成功调用所需的字符串。

我怎么了?

更新

:body => "{ 'requestAuditInfo': {  'agentCode': 'MLG001',
  'requestDateTime': '2018-07-29T07:03:38Z'

没有被接收 API 消化,返回 NIL 结果

标签: ruby-on-railsjsonrubyhttparty

解决方案


您正在发送字符串 "requestDateTime": Time.now.utc.iso8601 [...]" 作为参数。如果要将 rubyTime.now​​ 作为字符串的一部分传递,则需要对其进行插值,如下所示:

"requestDateTime": #{Time.now.utc.iso8601}"


推荐阅读