【问题标题】:Postman multipart POST to RailsPostman 多部分 POST 到 Rails
【发布时间】:2015-11-21 13:06:14
【问题描述】:

我正在尝试从某个客户端向 Rails 服务器发送 POST 请求,但遇到了一些问题。完整的要求是发送要由回形针处理的图像,但它看起来像是普通的邮递员多部分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

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

提前致谢。

编辑: 正如 fylooi 所建议的,我已经更改了 Postman 中的请求正文,添加了一个封装的“实体”,如下所示:

我仍然得到相同的结果

    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'

【问题讨论】:

    标签: ruby-on-rails multipartform-data postman


    【解决方案1】:

    Postman 与 Rails 配合得很好,您只需要了解 Rails 处理参数的一般方式即可。

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

    plain_param=value
    nested_object[attribute]=value
    

    这被解析成以下内容:

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

    让我们看看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"}}
    

    到目前为止,一切都很好。看起来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)
    

    我们可以看到require 返回单个参数键的值。这可以方便地确保存在具有多个属性的对象。

    总结:

    params.require(:category).permit(:label, :description)
    

    需要一个散列形式

    {:category=&gt;{:label=&gt;"value", :description=&gt;"value"}}

    转换为

    的 HTML POST 参数
    category[label]=value
    category[description]=value
    

    编辑:邮递员会自动设置content-type 标头以进行多部分文件上传,因此请勿手动设置。不确定这是否被视为错误或功能。

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

    【讨论】:

    • 您好,感谢您的回复。我已经尝试在 Postman 中使用 category[{attribute}] 设置键名,但我仍然遇到同样的错误。我也会用这个修改来编辑这个问题。谢谢
    • @Lori: 在params.require... 行之前添加puts params 并发布日志输出?
    • 这是参数的样子:{"------WebKitFormBoundaryaH47lDnnoGYytZoO\r\nContent-Disposition: form-data; name"=>"\"category[label]\"\r \n\r\n交通\r\n------WebKitFormBoundaryaH47lDnnoGYytZoO\r\nContent-Disposition: form-data; name=\"category[description]\"\r\n\r\n交通类别\r\ n------WebKitFormBoundaryaH47lDnnoGYytZoO--\r\n", "controller"=>"categories", "action"=>"create"}
    • @Lori:这是puts params打印的吗?
    • 如果您包含文件,Postman 将添加 content-type=multipart 标头。您不需要手动添加它。多部分表示法应该是 Rails 无法正确解析请求的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多