我正在尝试将 csv 文件上传到我的端点。我尝试这样做并导致 302 Found。尝试了此处提到的其他解决方案:Multipart POST Ruby HTTPS,但结果相同。
使用 net/http 的第一个选项
url = URI('https://abcd.com/test/upload')
out_file = '/Users/username/Downloads/test.csv'
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["username"] = 'abcd@test.com'
request["password"] = 'test2345'
form_data = [['file', File.open(out_file)]]
request.content_type = 'multipart/form-data'
request.set_form form_data, 'multipart/form-data'
response = https.request(request)
puts "Response from CRA is #{response.read_body}"
result:
#<Net::HTTPFound 302 Found readbody=true>
第二个选项使用“rest-client”和https://github.com/rest-client/rest-client中提到的步骤
out_file = '/Users/username/Downloads/test.csv'
url = "https://abcd.com/test/upload"
begin
response = RestClient.post(url,:file => File.new(out_file, 'rb'),:headers => { "username": "abcd@test.com", "password": "test2345"})
rescue RestClient::ExceptionWithResponse => err
end
result:
[319] pry(main)> err
=> #<RestClient::Found: 302 Found>
[320] pry(main)> err.response
=> <RestClient::Response 302 "<html><head...">
[321] pry(main)> err.response.follow_redirection
IOError: closed stream
[322] pry(main)>
response body:
"<html><head><title>Object moved</title></head><body>\r\n<h2>Object moved to <a href=\"/Account/Login?ReturnUrl=%2ftesy%2fuploads\">here</a>.</h2>\r\n</body></html>\r\n",
url=#<URI::HTTPS https://abcd.com/test/upload>
谁能帮我理解我做错了什么?