ruby 如何设置内容长度和内容类型头时,发送一个文件与HTTParty POST?

puruo6ea  于 2023-10-17  发布在  Ruby
关注(0)|答案(1)|浏览(133)

我把header设置成这样:其中文件是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})

请求头的设置如下:

<- "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和Content-Type被添加和复制,此外Transfer-Encoding被设置为chunked。
如何设置Content-Length、Content-Type和Transfer-Encoding并避免HTTParty自己设置它们?
希望没什么事。
Thx for your time!

8wtpewkr

8wtpewkr1#

试试这个

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

相关问题