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

时间:2016-04-27 14:55:25

标签: ruby http-headers content-type httparty content-length

我正在设置这样的标题: 其中file是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自行设置?

希望很清楚。

你的时间!

1 个答案:

答案 0 :(得分:0)

试试这个

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