Grape API-使用curl和data选项发出发布请求时出现问题

时间:2018-10-10 23:57:38

标签: ruby grape

我有一个用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

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我知道了。 -d发送内容类型application/x-www-form-urlencoded,这意味着我需要在标头中指定JSON内容类型,而我没有这样做。我要解决的是:

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