如何解决Ruby POST multipart/form-data 结果 302 Found
我正在尝试将 csv 文件上传到我的端点。我尝试这样做并导致 302 Found。 尝试了此处提到的其他解决方案:Multipart POST Ruby HTTPS 但结果相同。
使用 net/http 的第一个选项
Close()
使用 'rest-client' 的第二个选项和 https://github.com/rest-client/rest-client
中提到的步骤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>
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)>
谁能帮我理解我做错了什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。