繁体   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