如何使用HTTP POST上传多部分文件

时间:2016-12-16 17:33:11

标签: ruby request http-post

我需要发送带有标题的POST以及一个名为" _token"的参数。在我的请求正文中以及我要上传的图像。

这是Postman给我的代码,但它永远不会加载文件:

require 'uri' 
require 'net/http'

url = URI("https://myurl")

http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url) 
request["content-type"] = 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
request["cookie"] = 'mycookie'
request["cache-control"] = 'no-cache'
request["postman-token"] = '6031ee00-4553-6baf-c31c-c377a7185510'
request.body = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"_token\"\r\n\r\token\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"photo\";
filename=\"img.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"

response = http.request(request)
puts response.read_body

0 个答案:

没有答案