邮差多部分POST到Rails

Lor*_*ori 3 ruby-on-rails multipartform-data postman

我正在尝试从某个客户端发送一个POST请求到一个rails服务器,我遇到了一些问题.完整的要求是发送一个图像由paperclip处理,但它看起来像是一个普通的postman多部分POST与Rails问题.

这就是我得到的: 在此输入图像描述 贝娄我的设置:

class CategoriesController < ApplicationController

def create
  @category = Category.new(category_params)

  respond_to do |format|
    if @category.save
      format.html { redirect_to @category, notice: 'Category was successfully created.' }
      format.json { render :show, status: :created, location: @category }
    else
      format.html { render :new }
      format.json { render json: @category.errors, status: :unprocessable_entity }
    end
  end
end
  private

    def category_params
      params.require(:category).permit(:label, :description)
    end
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述

我假设问题是请求参数没有封装在"类别"中.如果我不够清楚,如果我能提供更多信息,请告诉我.

提前致谢.

编辑:正如fylooi所建议我改变了Postman中的Request Body,添加了一个封装的"实体",如下所示: 在此输入图像描述

我仍然得到相同的结果

    Processing by CategoriesController#create as JSON
  Parameters: {"------WebKitFormBoundaryFdJXZFMuAl0fZf3Q\r\nContent-Disposition: form-data; name"=>"\"category[label]\"\r\n\r\nTraffic\r\n------WebKitFormBoundaryFdJXZFMuAl0fZf3Q\r\nContent-Disposition: form-data; name=\"category[description]\"\r\n\r\nTraffic category\r\n------WebKitFormBoundaryFdJXZFMuAl0fZf3Q--\r\n"}
Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms)

ActionController::ParameterMissing (param is missing or the value is empty: category):
  app/controllers/categories_controller.rb:67:in `category_params'
  app/controllers/categories_controller.rb:27:in `create'
Run Code Online (Sandbox Code Playgroud)

fyl*_*ooi 6

Postman可以正常使用Rails,你只需要了解Rails如何处理参数.

假设您将以下参数POST到服务器:

plain_param=value
nested_object[attribute]=value
Run Code Online (Sandbox Code Playgroud)

这将被解析为以下内容:

pry(main)> params = ActionController::Parameters.new(plain_param:"value", nested_object: { attribute: "value" } )
=> {"plain_param"=>"value", "nested_object"=>{"attribute"=>"value"}}
Run Code Online (Sandbox Code Playgroud)

我们来看看它是如何permit工作的.

params.permit(:plain_param)
pry(main)> params.permit(:plain_param)
Unpermitted parameter: nested_object
=> {"plain_param"=>"value"}

pry(main)> params.permit(:nested_object)
Unpermitted parameters: plain_param, nested_object
=> {}

pry(main)> params.permit(:nested_object => :attribute)
Unpermitted parameter: plain_param
=> {"nested_object"=>{"attribute"=>"value"}}

pry(main)> params.permit(:plain_param, :nested_object => :attribute )
=> {"plain_param"=>"value", "nested_object"=>{"attribute"=>"value"}}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.看起来像permit返回顶级和嵌套允许键的整个哈希值,并打印未允许键的警报.怎么样require

[33] pry(main)> params
=> {"plain_param"=>"value", "nested_object"=>{"attribute"=>"value"}}

pry(main)> params.require(:plain_param)
=> "value"

pry(main)> params.require(:nested_object)
=> {"attribute"=>"value"}

pry(main)> params.require(:nested_object => :attribute)
ActionController::ParameterMissing: param is missing or the value is empty: {:nested_object=>:attribute}

pry(main)> params.require(:plain_param, :nested_object)
ArgumentError: wrong number of arguments (2 for 1)
Run Code Online (Sandbox Code Playgroud)

我们可以看到require返回单个param键的值.这样可以确保存在具有多个属性的对象.

包起来:

params.require(:category).permit(:label, :description)
Run Code Online (Sandbox Code Playgroud)

期望表格的哈希

{:category=>{:label=>"value", :description=>"value"}}

转换为HTML POST参数

category[label]=value
category[description]=value
Run Code Online (Sandbox Code Playgroud)

编辑:Postman自动设置content-type多部分文件上传的标题,因此不要手动设置.不确定这是否被视为错误或功能.

https://github.com/postmanlabs/postman-app-support/issues/191

  • 如果包含文件,Postman将添加`content-type = multipart`标头.您无需手动添加它.多部分表示法应该是Rails无法正确解析请求的原因. (2认同)