converting time to digestible JSON string with HTTParty

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


converting time to digestible JSON string with HTTParty



The following string returns results


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



whereas


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



returns nil



Console for Time.now.utc.iso8601 returns "2018-07-29T07:03:38Z" which is the desired string for the successful call.


Time.now.utc.iso8601


"2018-07-29T07:03:38Z"



What am I getting wrong?



update


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



is NOT being digested by the receiving API, returning NIL results





have you tried using strftime?
– Mark Merritt
4 hours ago


strftime





no, as I was attempting to use the iso8601 pre-set format
– Jerome
4 hours ago





interpolation is missing in your quotes. Ruby code needs to be interpolated with #{} when used in string. Replace with #{Time.now.utc.iso8601} and use double quotation - "{ 'requestAuditInfo': { 'agentCode': 'MLG001', 'requestDateTime': #{Time.now.utc.iso8601} [...] or use %Q as :body => %Q|{ "requestAuditInfo": { "agentCode": "MLG001", #{Time.now.utc.iso8601} [...]|
– kiddorails
3 hours ago




#{}


#{Time.now.utc.iso8601}


"{ 'requestAuditInfo': { 'agentCode': 'MLG001', 'requestDateTime': #{Time.now.utc.iso8601} [...]


%Q


:body => %Q|{ "requestAuditInfo": { "agentCode": "MLG001", #{Time.now.utc.iso8601} [...]|





the double quotation I had tried, but as the update shows, it is not arriving properly to server. the %Q| [...] | option runs with the string "2018-07-29T07:03:38Z" , but not the interpolation.
– Jerome
2 hours ago


%Q| [...] |


"2018-07-29T07:03:38Z"





in CLI the quote handling is confirmed: Unexpected character (''' (code 39)): was expecting double-quote to start field name
– Jerome
2 hours ago


Unexpected character (''' (code 39)): was expecting double-quote to start field name




1 Answer
1



You are sending a string "requestDateTime": Time.now.utc.iso8601 [...]" as a parameter. If you want to pass ruby's Time.now as part of a string you need to interpolate it, like so:


Time.now



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


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Regex - How to capture all iterations of a repeating pattern?