如何使用Postman创建对Rails API的Post请求?

izj3ouym  于 2022-12-13  发布在  Postman
关注(0)|答案(3)|浏览(200)

我是新来的 Postman 。我有一个Rails服务器在后台运行。我试图模拟一个POST请求,但它没有被接受。
假设模型名为manufacturer_organization.rb,在内部,它 * 需要 * 3个参数:organization_id (uuid data type), manufacturer_id (integer data type), and account_number (string data type).manufacturer_organization属于组织,也属于:制造商(反之亦然;制造商和组织有_多个制造商_组织)
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序列化器,并且我也为这个模型设置了序列化器。Route也是使用resources :manufacturer_organizations设置的。我的直觉告诉我,我使用postman的方式是错误的。
这是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")
如何从 Postman 提交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

为什么是空白的?

j9per5c4

j9per5c41#

您可以使用params或在body请求中传递数据。
最好的方法是使用主体,因为您可以发送文件,并且请求在没有参数的情况下会变得更干净。
要在正文中发送数据,必须在“key”字段中传递模型名称和属性,并在“value”字段中传递值,如下所示:

z8dt9xmd

z8dt9xmd2#

我不明白您对 params 做了什么。ActiveModelSerializers::Deserialization 在“Model”命名空间中命名是有原因的。它不应该用于序列化或反序列化Internet参数,而是用于序列化/反序列化模型示例。
如果参数以正确的格式到达,则 AplicationControllerManufacturerOrganizationsController 继承的 ActionController::Base 将为您反序列化它们。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不是一个包含所有应该反序列化的参数的字符串,它应该已经被反序列化了,如果不是这样,那么你可能发送了错误的参数.

sd2nnvve

sd2nnvve3#

Ruby on Rails和 Postman -发布请求。

你好,这是我用Postman和Rails API开发的一个例子。
Postman
我不能添加图像,但这是你必须在postman中添加的=更改为发布请求并发送。

book[name] = 'Harry Potter'
book[author] = J.K. Rowling

第七章.
Rails维护相同的代码。

def create
    @book = Book.new(book_params)

    if @book.save
      render json: @book, status: :created, location: api_v1_books_url(@book)
    else
      render json: @book.errors, status: :unprocessable_entity
    end
  end

  def book_params
    debugger
    params.require(:book).permit(:name, :author, :price)
  end

我希望这能帮上忙。

相关问题