ruby-on-rails - 我正在使用 POST 方法将文件传输到给定的 URL,并在 rails 中出现错误
问题描述
{
"body": "Unsupported method.",
"code": "400",
"message": "",
"res": {
"connection": [
"close"
],
"content-length": [
"19"
],
"content-type": [
"text;charset=ISO-8859-1"
],
"date": [
"Mon, 20 Sep 2021 05:37:36 GMT"
]
}
}
以上错误进入Airbrake
uri = URI(URL it is constant)
req = Net::HTTP::Post.new(uri)
req.set_form(['upload', File.open("#{file_name}")], 'multipart/form-data')
req.set_form_data("mac" => mac)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
res = http.request(req)
case res
when Net::HTTPSuccess, Net::HTTPRedirection
Rails.logger.info "File sent successgully"
# OK
else
Airbrake.notify("File transfer failed !", {code: res.code, message: res.message, body: res.body, res: res})
解决方案
这对我有用
uri = URI.parse(RAKUTEN_URL)
File.open("#{file_name}") do |transfile|
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Post::Multipart.new(
uri.path,
mac: encoded_mac,
file: UploadIO.new(transfile, "multipart/formdata", File.basename("#{file_name}")),
)
response = http.request(req)
case response
when Net::HTTPSuccess, Net::HTTPRedirection
#OK
Rails.logger.info("File sent successfully. Don't panic its only for testing ! #{response}")
else
Airbrake.notify("File transfer failed ! #{response}")
end
end
end
推荐阅读
- mysql - 如何在 MySQL FULLTEXT 索引字段中使某些标点字符可索引
- python - Init.py 用于本地 settings.py 而不是生产 settings.py DJANGO
- flutter - 如何在 ListView 中添加 Spacer
- ios - 购买促销优惠(苹果):签名无效(SKErrorDomain 错误 12。)
- javascript - 使用 jasmine 测试 Array.prototype.slice.call 返回值
- flutter - 通过拖动停用鼠标滚动,但保持触摸滚动激活
- r - SankeyNetwork随着时间的推移
- kubernetes - Kubernetes 机密作为环境变量添加空格字符
- ios - 为什么 UIViewController 是从同名 XIB 创建的,即使使用了空构造函数?
- oracle - 动态操作之前的 Oracle 顶点表单验证