葡萄 API - 使用带有数据的 curl 选项发出发布请求时出现问题



我有一个用 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发送内容类型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

最新更新