简体   繁体   English

邮差多部分POST到Rails

[英]Postman multipart POST to Rails

I'm trying to send a POST request from some client to a rails server and I'm having some problems.The full requirement is to send an image to to be processed by paperclip but it look like it's a general postman multipart POST with Rails problem. 我正试图从一些客户端发送一个POST请求到一个rails服务器,我遇到了一些问题。完整的要求是发送一个图像由paperclip处理,但它看起来像是一个普通的postman多部分POST与Rails问题。

This is what I'm getting: 这就是我得到的: 在此输入图像描述 Bellow my setup: 贝娄我的设置:

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

在此输入图像描述

在此输入图像描述

I'm assuming the problem is that the Request params are not encapsulated int the "categories". 我假设问题是请求参数没有封装在“类别”中。 Please let me know if I wasn't clear enough and if I can offer more info. 如果我不够清楚,如果我能提供更多信息,请告诉我。

Thanks in advance. 提前致谢。

EDIT: As suggested by fylooi I've changed the Request Body in Postman adding an encapsulating "entity" like this: 编辑:正如fylooi所建议我改变了Postman中的Request Body,添加了一个封装的“实体”,如下所示: 在此输入图像描述

Still I'm getting the same results 我仍然得到相同的结果

    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'

Postman works fine with Rails, you just need to understand how Rails handles parameters in general. Postman可以正常使用Rails,你只需要了解Rails如何处理参数。

Let's say you POST the following parameters to the server: 假设您将以下参数POST到服务器:

plain_param=value
nested_object[attribute]=value

This gets parsed into the following: 这将被解析为以下内容:

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

Let's take a look at how permit works. 我们来看看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"}}

So far, so good. 到现在为止还挺好。 Looks like permit returns the entire hash for top level and nested permitted keys through and prints an alert for unpermitted keys. 看起来像permit返回顶级和嵌套允许键的整个哈希,并打印未允许键的警报。 How about require ? 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)

We can see that require returns the value for a single param key. 我们可以看到require返回单个param键的值。 This comes in handy to ensure the presence of objects with multiple attributes. 这样可以确保存在具有多个属性的对象。

Wrapping up: 包起来:

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

expects a hash of the form 期望表格的哈希

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

which translates to HTML POST parameters of 转换为HTML POST参数

category[label]=value
category[description]=value

Edit: Postman automagically sets the content-type header for multi part file upload, so do not set it manually. 编辑:Postman自动设置多部分文件上载的content-type标题,因此不要手动设置。 Not sure whether this is considered a bug or a feature. 不确定这是否被视为错误或功能。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM