1

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!

metadaddy
  • 4,234
  • 1
  • 22
  • 46
Adax
  • 56
  • 1
  • 6
  • What do you try to achieve by setting these headers yourself? – spickermann Apr 27 '16 at 15:17
  • I'm trying to use Backblaze.com api to upload a file. Their documentation asks for such. There is a working solution in "raw" net/http in there. I have it working with that, but i would like to use httparty. https://www.backblaze.com/b2/docs/b2_upload_file.html – Adax Apr 27 '16 at 15:21

1 Answers1

0

Try this

HTTParty.post(END_POINT_URL, 
:body => data.to_json,
:headers => { 'Content-Type' => 'application/json', 
'Content-Length' => content_length, 'Other-Header' => other_header, } )    
r3b00t
  • 6,953
  • 6
  • 27
  • 37
  • Thx for the answer. But i need to set the content type 'text/plain' and send the body as such, not as JSON. I'm uploading a file to backblaze: (backblaze.com/b2/docs/b2_upload_file.html). Transfer-Encoding gets set to chunked and I need to avoid it. – Adax May 07 '16 at 03:32