Ruby on Rails调用Podium API上传附件报错:File type is not supported
Podium v4 Messages Attachment API 文件类型不支持问题解决
问题场景
在Ruby on Rails项目中调用Podium v4版本的messages/attachment API上传PNG截图时,返回错误响应:
{"code"=>"invalid_request_values", "message"=>"File type is not supported.", "moreInfo"=>"https://docs.podium.com/docs/errors#invalid_request_values"}
调用代码如下:
url = URI("https://api.podium.com/v4/messages/attachment") https = Net::HTTP.new(url.host, url.port) https.use_ssl = true request = Net::HTTP::Post.new(url) request["Content-Type"] = "multipart/form-data" request["Authorization"] = "Bearer #{access_token}" form_data = [["attachment",File.open('D:\proj\v5\ap\fl\Screenshot (1).png')],['data', "#{request_data}"]] request.set_form(form_data, 'multipart/form-data') response = https.request(request) response_body = JSON.parse(response.body) if response.code == '200' || response.code == '201' return response_body,'success' else return response_body,"#{response.message}" end rescue Exception => ex return ex,'Exception' end
解决步骤
手动指定文件Content-Type
核心问题是Net::HTTP未正确识别PNG文件的MIME类型,导致API收到的文件类型标记不符合要求。修改form_data定义,给附件添加明确的Content-Type参数:form_data = [ ["attachment", File.open('D:\proj\v5\ap\fl\Screenshot (1).png'), { 'Content-Type' => 'image/png' }], ['data', "#{request_data}"] ]验证文件有效性
确认本地文件为未损坏的标准PNG格式:- 用系统
file命令检查:file "D:\proj\v5\ap\fl\Screenshot (1).png",输出应包含PNG image data; - Ruby中用
MIME::Typesgem检测:require 'mime/types' mime_type = MIME::Types.type_for('D:\proj\v5\ap\fl\Screenshot (1).png').first.content_type # 正常应返回"image/png"
- 用系统
检查
data参数格式
确保request_data是符合API要求的JSON格式,避免因该参数格式错误触发关联的文件类型校验失败。
内容的提问来源于stack exchange,提问作者Ashish Sharma
相关产品推荐
相关产品推荐

