如何在葡萄摇摇欲坠的轨道上上传文件? 错误:请求的格式' txt'不支持

问题描述

我需要使用Grape UI实施文件上传。我们有这些宝石:

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 &#39;txt&#39; 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']