【问题标题】:How to create Post request to Rails API using Postman?如何使用 Postman 创建对 Rails API 的 Post 请求?
【发布时间】:2017-09-10 05:39:53
【问题描述】:

我是 Postman 的新手。我有一个在后台运行的 Rails 服务器。我正在尝试模拟一个 POST 请求,但它没有被接受。

假设模型名为manufacturer_organization.rb。在内部,它需要 3 个参数:organization_id (uuid data type), manufacturer_id (integer data type), and account_number (string data type).manufacturer_organizationbelongs_to 组织和它还属于_to :manufacturer(反之亦然;制造商和组织有_many 制造商_组织)

manufacturer_organizations_controller.rb里面,我有一个create方法:

  def create
    @manufacturer_organization = ManufacturerOrganization.new(manufacturer_organization_params)
    if @manufacturer_organization.save
      puts "success!"
      render json: @manufacturer_organization
    else
      puts "Sorry, something went wrong"
    end
  end

我可以确认我有足够的授权;当我执行 GET 请求时,我得到了正确的 JSON 响应。我正在使用 rails 序列化程序,并且我也为此模型设置了序列化程序。路线也使用resources :manufacturer_organizations 设置。我的直觉表明我使用邮递员的方式是错误的。

这是 Postman 应用程序的屏幕截图。我在地址栏上有正确的地址,我正在执行一个 POST 请求。我在key-value 下有三个参数。

在我Send 之后,在我的 Rails 服务器日志下我看到:

Started POST "/manufacturer_organizations" for 127.0.0.1 at 2017-04-13 16:56:44 -0700
Processing by ManufacturerOrganizationsController#create as */*
  Parameters: {"organization_id"=>"fb20ddc9-a3ee-47c3-bdd2-f710541-ff89c", "manufacturer_id"=>"1", "account_number"=>"A rand
om account number test"}
...
  (0.4ms)  BEGIN
   (0.3ms)  ROLLBACK
Sorry, something went wrong

我可以在rails console 内做ManufacturerOrganization.new(organization_id: Organization.last.id, manufacturer_id: Manufacturer.last.id, and account_number: "random test account number")

如何提交邮递员的 POST 请求以添加新的制造商组织?

编辑:

  def manufacturer_organization_params
    api_params.permit(:organization_id, :manufacturer_id, :account_number)
  end

而在application_controller.rb内部

def api_params
    @api_params ||= ActionController::Parameters.new(ActiveModelSerializers::Deserialization.jsonapi_parse(params))
  end

编辑2:

我添加了error.full_messages,这就是我得到的:

Manufacturer can't be blank
Organization can't be blank
Account number can't be blank

为什么它们是空白的?

【问题讨论】:

  • 请显示您的manufacturer_organization_params
  • 嗯,看起来不错。并且 params 从邮递员那里顺利通过 - 我认为邮递员没有问题
  • 在 else 中打印实际错误而不是“抱歉”消息,看看问题出在哪里。
  • 把@manufacturer_organization_params 看一下ManufacturerOrganization 的初始化方法
  • 抱歉,您介意详细说明一下吗?我在控制器上添加了puts manufacturer_organization_params,它打印出<ActionController::Parameters:0x007fc1ce415b80>,而当我提交POST时添加puts @manufacturer_organization_params什么也不打印。

标签: ruby-on-rails api postman


【解决方案1】:

您可以使用参数或在正文请求中传递数据。

最好的方法是使用正文,因为您可以发送文件,并且请求在没有参数的情况下变得更加干净。

要在正文中发送数据,必须在“key”字段中传递模型名称和属性,在“value”字段中传递值,如下所示:

【讨论】:

    【解决方案2】:

    我不明白你对你的参数做了什么。 ActiveModelSerializers::Deserialization 在“模型”命名空间中命名是有原因的。它不应该用于序列化或反序列化互联网参数,而是用于序列化/反序列化模型实例。

    如果参数以正确的格式 ActionController::Base 到达,AplicationController 和因此 ManufacturerOrganizationsController 继承的格式将为您反序列化。 Rails 查询参数格式如下:

    name=something                        #=> params[:name] = 'something'
    
    names[]=something1&names[]=something2 #=> params[:names] = ['something1', 'something2']
    
    instance[id]=1&instance[name]=foo     #=> params[:instance] = {id: '1', name: 'foo'}
    

    这也可以堆叠并被 Rails 用于嵌套资源。示例:

    instance[title]=some&instance[nested][name]=thing&instance[nested][ids][]=1&instance[nested][ids][]=2
    #=> params[:instance] = {title: 'some', nested: {name: 'thing', ids: ['1', '2']}}
    

    话虽如此,让我们来看看你的例子。首先让我们抛弃那些手动构建的参数并遵守约定:

    class ManufacturerOrganizationsController
    
      # ...
    
      private
    
      def manufacturer_organization_params
        # arriving params should look like this:
        #
        #=> params = {
        #     manufacturer_organization: {
        #       organization_id: 'fb20ddc9-a3ee-47c3-bdd2-f710541-ff89c',
        #       organization_id: '1',
        #       account_number: 'A random account number test'
        #     }
        #   }
        #
        # The method #require raises an exception if the provided key
        # is not present or has a blank value (with exception of false).
        # If the key is found and has a value present than that value is
        # returned.
        #
        params.require(:manufacturer_organization)
              .permit(:organization_id, :manufacturer_id, :account_number)
      end
    
    end
    

    让我们发送正确格式的参数:

    +--------------------------------------------+---------------------------------------+
    | Key                                        | Value                                 |
    |--------------------------------------------|---------------------------------------|
    | manufacturer_organization[organization_id] | fb20ddc9-a3ee-47c3-bdd2-f710541-ff89c |
    | manufacturer_organization[manufacturer_id] | 1                                     |
    | manufacturer_organization[account_number]  | A random account number test          |
    +--------------------------------------------+---------------------------------------+
    

    这两件事结合起来应该可以让您成功创建资源。

    你应该从中得到的关键是 params 不是一个包含所有应该反序列化的参数的字符串。它已经应该被反序列化,如果不是你可能发送错误的参数。

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 2021-03-23
      • 2021-10-05
      • 2021-12-26
      • 2014-11-19
      • 1970-01-01
      • 2019-01-09
      • 2019-10-11
      • 2020-08-01
      相关资源
      最近更新 更多