Steve Q
Steve Q

Reputation: 395

HTTParty integer in request headers

I need to POST to an API, which requires a header in the request called "Timestamp", whose value has to be an integer (current time from epoch as an integer).

I tried to do this with HTTParty and with Net::HTTP as follows:

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

It calls strip on the header value, which throws an error because you can't call strip on an integer.

Does anyone have any idea how I can pass an integer in the request headers?

Upvotes: 2

Views: 2503

Answers (1)

max
max

Reputation: 101901

Call .to_s on the integer:

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' })

You can't really send an integer in a header - its the server on the receiving end that must convert the plaintext headers from the request.

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

Upvotes: 2

Related Questions