我需要使用net / http类中的方法“ set_form”将Content-type = "application/pdf"
设置为请求的参数,但它始终向我显示Content-Type =“ application / octet-stream”。
我已经签出documentation for set_form,但是我不知道如何正确设置Content-Type。
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new("#{path}")
request.set_form([
["parameter1","#{parameter1}"],
["parameter2","#{parameter2}"],
["attachment", File.new("/home/name.pdf", "rb")]
], 'multipart/form-data')
response = http.request(request)
我已经尝试使用此代码来设置像文档一样的opt哈希,但是我收到了相同的响应:
uri = URI.parse("#{host}")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new("#{path}")
request.set_form([
["parameter1","#{parameter1}"],
["parameter2","#{parameter2}"],
["attachment", File.new("/home/name.pdf", "rb"), { "Content-Type" => "application/pdf" }]
], 'multipart/form-data')
response = http.request(request)
实际输出仍然是Content-Type: application/octet-stream
,但我需要:
... Content-Disposition: form-data; name=\"attachment\"; filename=\"name.pdf\"\r\nContent-Type: application/pdf\r\n\r\n%PDF-1.5\r%\xE2\xE ...
我发现了这个(https://dradisframework.com/blog/2017/04/dradis-attachments-api-using-ruby/),不确定是否有帮助。
此代码为请求构建各个身体部位。
require 'net/http'
uri = URI('http://dradis.ip/pro/api/nodes/18/attachments')
Net::HTTP.start(uri.host, uri.port) do |http|
# Read attachments associated to a specific node:
get_request = Net::HTTP::Get.new uri
get_request['Authorization'] = 'Token token="iOEFCQDR-miTHNTjiBxObjWC"'
get_request['Dradis-Project-Id'] = '8'
get_response = http.request(get_request)
puts get_response.body
# Attach some other files to that node:
BOUNDARY = "AaB03x"
file1 = '/your/local/path/image1.png'
file2 = '/your/local/path/image2.png'
post_body = []
post_body << "--#{BOUNDARY}\r\n"
post_body << "Content-Disposition: form-data; name=\"files[]\"; filename=\"#{File.basename(file1)}\"\r\n"
post_body << "Content-Type: image/png\r\n"
post_body << "\r\n"
post_body << File.read(file1)
post_body << "\r\n--#{BOUNDARY}\r\n"
post_body << "Content-Disposition: form-data; name=\"files[]\"; filename=\"#{File.basename(file2)}\"\r\n"
post_body << "Content-Type: image/png\r\n"
post_body << "\r\n"
post_body << File.read(file2)
post_body << "\r\n--#{BOUNDARY}--\r\n"
post_request = Net::HTTP::Post.new uri
post_request['Authorization'] = 'Token token="iOEFCQDR-miTHNTjiBxObjWC"'
post_request['Dradis-Project-Id'] = '8'
post_request.body = post_body.join
post_request["Content-Type"] = "multipart/form-data, boundary=#{BOUNDARY}"
post_response = http.request(post_request)
puts post_response.body
end
我认为,对于多部分请求,这是不可能的,正如您在code for the set_form
method中看到的那样,当设置了不同的内容类型时,它将引发错误。
set_form
肯定有其他我认为的解决方法,或者您可以简单地使用不同的http客户端(如HTTParty)来支持多部分请求的不同内容类型,开始读取# File net/http/header.rb, line 452
def set_form(params, enctype='application/x-www-form-urlencoded', formopt={})
@body_data = params
@body = nil
@body_stream = nil
@form_option = formopt
case enctype
when /\Aapplication\/x-www-form-urlencoded\z/i,
/\Amultipart\/form-data\z/i
self.content_type = enctype
else
raise ArgumentError, "invalid enctype: #{enctype}"
end
end
,然后继续输入the example here。我没有找到更好的文档。