Search code examples
ruby-on-railsrubyhttparty

HTTParty request failing when setting headers


I'm trying to upload a video to Vimeo. If I leave the headers empty and only pass the authorization token, it works fine. When I set the content length header the request is failing. Any ideas on how to properly create a request header?

The method I created for adding headers:

 def add_header(key, value)
   @headers[key.to_sym] = value
 end

The way I'm adding content-type header:

add_header('content-type','video/mp4')

When I comment out this line, the requests works fine, but it's required by Vimeo. When debugging, I found the has to HTTParty is formed like this, could this be causing the error:

{:headers=>{:authorization=>"Bearer xxxxx", :"Content-Type"=>"video/mp4", :"Content-Length"=>"54047"}

Solution

  • Use strings for headers keys instead of symbols. Remove .to_sym in add_header method

    def add_header(key, value)
      @headers[key] = value
    end