簡體   English   中英

Grape API-使用curl和data選項發出發布請求時出現問題

[英]Grape API - problem making a post request using curl with data option

我有一個用Grape編寫的終結點,該終結點繼承自基類,如下所示:

module API
  class Core < Grape::API
    default_format :json
    prefix :api
    content_type :json, 'application/json'

    mount ::Trips::Base
  end
end

這是我的終點:

module Trips
  class TripsAPI < API::Core
    helpers do
      params :trips_params do
        requires :start_address, type: String
        requires :destination_address, type: String
        requires :price, type: Float
        requires :date, type: Date
      end
    end

    resources :trips do
      params do
        use :trips_params
      end
      desc 'Creates new ride'
      post do
        Rides::CreateRide.new(params).call
      end
    end
  end
end

當我發出明確的發布請求時,它可以正常工作。

curl -d "start_address=some address&destination_address=some address&price=120&date=10.10.2018" -X POST http://localhost:3000/api/trips

當我嘗試使用帶有-d選項的curl發出發布請求時,收到錯誤消息: {"error":"start_address is missing, destination_address is missing, price is missing, date is missing"}

curl -i -H "Accept: application/vnd.api+json" -X POST -d '{ "start_address": "asdasd", "destination_address": "asdasdada", "price": 120, "date": "10.10.2018" }' http://localhost:3000/api/trips

我究竟做錯了什么?

我想通了。 -d發送Content-Type application/x-www-form-urlencoded ,這意味着我需要在標頭中指定JSON Content-Type,而我沒有這樣做。 我要做的是解決:

curl -i -H "Content-Type: application/json" -X POST -d '{ "start_address": "asdasd", "destination_address": "asdasdada", "price": 120, "date": "10.10.2018" }' http://localhost:3000/api/trips

暫無
暫無

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

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