使用 HTTParty POST 发送文件时如何设置 Content-Length 和 Content-Type 标头?

问题描述 投票:0回答:1

我设置标题如下: 其中文件是 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 并避免 HTTParty 自行设置它们?

希望很清楚。

感谢您的宝贵时间!

ruby content-type httparty content-length backblaze
1个回答
0
投票

试试这个

HTTParty.post(END_POINT_URL, 
:body => data.to_json,
:headers => { 'Content-Type' => 'application/json', 
'Content-Length' => content_length, 'Other-Header' => other_header, } )    
© www.soinside.com 2019 - 2024. All rights reserved.