ruby - Ruby POST multipart/form-data 导致 302 Found
问题描述
我正在尝试将 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>
谁能帮我理解我做错了什么?
解决方案
推荐阅读
- php - 如何制作一个安全的登录系统?
- flutter - 有机会在 Flutter 上使用 ListView 禁用延迟加载吗?
- reactjs - React 组件中的单一职责
- mysql - 当前行为空时使用上一行结果
- c - 为什么此代码显示此“控制到达非无效函数 [-Werror=return-type] 的末尾”错误?
- python - Fortran 中的不精确计算
- django - 1 个通用 api 视图中的 2 个 django 序列化程序
- database - 提供智能手机和 Web 应用程序时如何确保本地和远程数据库之间的数据一致性
- javascript - Node js 共享变量和多个用户和多个入口点
- sql - 如何将此条件从 SQL 转换为 Django