簡體   English   中英

如何使用 Postman 創建對 Rails API 的 Post 請求?

[英]How to create Post request to Rails API using Postman?

我是 Postman 的新手。我有一個后台運行的 Rails 服務器。 我正在嘗試模擬 POST 請求,但未被接受。

假設 model 稱為manufacturer_organization.rb 在內部,它需要3 個參數: organization_id (uuid data type), manufacturer_id (integer data type), and account_number (string data type). manufacturer_organization belongs_to organization and it also belongs_to:manufacturer (反之亦然;制造商和組織 has_many manufacturer_organization)

manufacturer_organizations_controller.rb中,我有一個創建方法:

  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 序列化程序,我也為此 model 設置了序列化程序。 路線也使用resources:manufacturer_organizations設置。 我的直覺說我使用 postman 的方式是錯誤的。

這是 Postman 應用程序的屏幕截圖。 我在地址欄上有正確的地址,我正在執行 POST 請求。 我在key-value下有三個參數。

郵遞員截圖 POST

Send后,在我的 Rails Server 日志下我看到:

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 請求以添加新的 manufacturer_organization?

編輯:

  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

為什么它們是空白的?

您可以使用params或在body請求中傳遞數據。

執行此操作的最佳方法是使用正文,因為您可以發送文件,並且請求在沒有參數的情況下變得更加干凈。

要在正文中發送數據,您必須在“key”字段中傳遞模型名稱和屬性,並在“value”字段中傳遞值,如下所示:

請參見此示例圖像

我不明白你對你的params做了什么。 ActiveModelSerializers :: Deserialization在“Model”命名空間中被命名空間是有原因的。 它不應該用於序列化或反序列化互聯網參數,而是用於序列化/反序列化模型實例。

如果參數以正確的格式到達,那么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不是包含應該反序列化的params的字符串。 它已經應該被反序列化,如果它不是你可能發送你的參數錯誤。

Ruby on Rails 和 Postman - 發布請求。

你好,這是我用Postman和Rails API開發的一個例子。

Postman。

我無法添加圖像,但這是您必須在 postman Key = Value中添加的內容更改為發布請求並發送。

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

Ruby 在 Rails 7 上。

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

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM