How to set Content-Length and Content-Type headers when sending a file with a HTTParty POST?

2.8k Views Asked by At

I'm setting the headers like this: where file is a test.txt

size = File.size(file)

h = {
  "Content-Type":      'text/plain'
  "Content-Length":     size.to_s,
  "Other-Header":      'some-header'     
 }

b = File.read(file)

HTTParty.post('/some/api/url', {headers: h , body: b})

The request headers get set like this:

<- "POST /some/api/url\r\n
Content-Type: text/plain\r\n  
Content-Length: 16\r\n
Other-Header: some-header\r\n
Connection: close\r\n
Host: somehost.com\r\n
Content-Length: 16\r\n
Content-Type: application/x-www-form-urlencoded\r\n\r\n"

Content-Length and Content-Type are added and duplicated, besides Transfer-Encoding gets set to chunked.

How can one set Content-Length, Content-Type and Transfer-Encoding and avoid HTTParty setting them on its own?

Hope it's clear.

Thx for your time!

1

There are 1 best solutions below

1
On

Try this

HTTParty.post(END_POINT_URL, 
:body => data.to_json,
:headers => { 'Content-Type' => 'application/json', 
'Content-Length' => content_length, 'Other-Header' => other_header, } )