问题描述
gem 'grape'
gem 'grape-entity'
gem 'grape-papertrail'
gem 'grape-swagger'
gem 'grape-swagger-entity'
在我的 attachments_api.rb 中:
# frozen_string_literal: true
module V1
class AttachmentsAPI < ApplicationAPI
content_type :pdf,'multipart/form-data'
resource :attachments do
desc 'Upload attchment file'
params do
requires :file,type: File,documentation: { param_type: 'formData',data_type: 'file' }
end
post do
byebug
end
end
end
end
但是当我在UI(https://editor.swagger.io/)中运行它时,每次都会遇到相同的错误(无论我尝试附加的txt文件还是png或pdf):
The requested format 'txt' is not supported.
更多奇怪的外观生成了json_doc:
paths:
/attachments:
post:
description: Upload attchment file
produces:
- application/json
consumes:
- application/json
parameters:
- in: formData
name: file
type: file
required: true
responses:
'201':
description: Upload attchment file
tags:
- attachments
operationId: postAttachments
我不明白为什么我们在那里看到
consumes:
- application/json
解决方法
这解决了问题:
desc 'Upload attachment file',consumes: ['multipart/form-data']