Kong / unirest-ruby

Unirest in Ruby: Simplified, lightweight HTTP client library.
http://unirest.io/ruby
MIT License
364 stars 81 forks source link

Unirest headers not interpreted correctly #37

Closed ishuah closed 7 years ago

ishuah commented 8 years ago

I'm trying to post JSON data but the host is interpreting the request as XML. I tested with httpbin.org, results below.

The post request: Unirest.post("http://httpbin.org/post", headers:{ "Content-Type" => "application/json" }, parameters: { "a"=> "b"} )

The response:

{"args"=>{}, "data"=>"", "files"=>{}, "form"=>{}, "headers"=>{"Accept"=>"*/*; q=0.5, 
application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"0", 
"Content-Type"=>"application/x-www-form-urlencoded", "Headers"=>
"{\"Content-Type\"=>\"application/json\"}", "Host"=>"httpbin.org", "Parameters"=>"{\"Inputs\"=>
{\"a\"=>\"b\"}}", "User-Agent"=>"unirest-ruby/1.0"}, "json"=>nil, 
"origin"=>"41.203.219.198", "url"=>"http://httpbin.org/post"}

The Headers key (uppercase h) contains the correct header values, while the headers key (lowercase h) contains different header values. The headers key is the one that's being accepted.

jasonpang commented 8 years ago

I'm also experiencing the same behavior with the same code.

va3093 commented 8 years ago

I am also having this problem 😞

iCreateJB commented 8 years ago

A bit late but the issue is that in your sample it didn't look like you were actually sending it JSON even though you set the content type.

2.2.2 :004 > Unirest.post("http://httpbin.org/post", headers:{ "Content-Type" => "application/json" }, parameters: { "a"=> "b"}.to_json ).body
 => {"args"=>{}, "data"=>"{\"a\":\"b\"}", "files"=>{}, "form"=>{}, "headers"=>{"Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip", "Content-Length"=>"9", "Content-Type"=>"application/json", "Host"=>"httpbin.org", "User-Agent"=>"unirest-ruby/1.1"}, "json"=>{"a"=>"b"}, "origin"=>"174.104.207.158", "url"=>"http://httpbin.org/post"} 
2.2.2 :005 > Unirest.post("http://httpbin.org/post", headers:{ "Content-Type" => "application/json" }, parameters: { "a"=> "b"} ).body
 => {"args"=>{}, "data"=>"", "files"=>{}, "form"=>{"a"=>"b"}, "headers"=>{"Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip", "Content-Length"=>"3", "Content-Type"=>"application/x-www-form-urlencoded", "Host"=>"httpbin.org", "User-Agent"=>"unirest-ruby/1.1"}, "json"=>nil, "origin"=>"174.104.207.158", "url"=>"http://httpbin.org/post"} 

cc @nikz

ishuah commented 7 years ago

@iCreateJB thanks for responding. I know it's almost a year gone but I like to see things resolved. I've just tested with version 1.0.8 and it looks good. Closing this issue.