在Ruby中向rest客户端请求添加数据字段
我在使用rest客户端gem向uber api发出请求时遇到问题。我认为问题在于我试图将所需信息作为参数传递到url中,而不是数据字段中。示例curl请求的参数在在Ruby中向rest客户端请求添加数据字段,ruby,rest-client,uber-api,Ruby,Rest Client,Uber Api,我在使用rest客户端gem向uber api发出请求时遇到问题。我认为问题在于我试图将所需信息作为参数传递到url中,而不是数据字段中。示例curl请求的参数在-d之后,但我不知道如何用rest客户机实现它。谢谢你的帮助 此请求在控制台中工作: curl -v -H "Authorization: Bearer " + session[:request_token] \ -H "Content-Type: application/json" -X POST -d \ '{"start
-d
之后,但我不知道如何用rest客户机实现它。谢谢你的帮助
此请求在控制台中工作:
curl -v -H "Authorization: Bearer " + session[:request_token] \
-H "Content-Type: application/json" -X POST -d \ '{"start_latitude":"37.334381","start_longitude":"-121.89432","end_latitude":"37.77703","end_longitude":"-122.419571","product_id":"a1111c8c-c720-46c3-8534-2fcdd730040d"}' \
https://sandbox-api.uber.com/v1/requests
来自控制器的请求,错误为406,响应不可接受
@uber_ride = JSON.load(RestClient::Request.execute(
:method => :post,
:url => "https://sandbox-api.uber.com/v1/sandbox/requests/product_id=#{@uberx_id}?start_latitude=#{lat_start}&start_longitude=#{lng_start}&end_latitude=#{lat_end}&end_longitude=#{lng_end}",
:headers => {'Authorization' => 'Bearer ' + session[:request_token], 'content_type' => 'application/json'}
))
我尝试为数据添加一个附加字段,但这会导致404 Resource Not Found错误
@uber_ride = JSON.load(RestClient::Request.execute(
:method => :post,
:url => "https://sandbox-api.uber.com/v1/sandbox/requests/",
:data => {'start_latitude' => lat_start, 'start_longitude' => lng_start, 'end_latitude' => lat_end, 'end_longitude' => lng_end, 'product_id' => @uberx_id},
:headers => {'Authorization' => 'Bearer ' + session[:request_token]},
:content_type => :json
))
我终于让它工作了。感谢@mudasobwa建议将data=>
更改为payload=>
。我还必须在payload对象周围添加单引号,这样rest客户机gem就不会将其转换为json。例如:'{}'
。最后,我必须更改指定内容类型的方式,并在:headers
中将其指定为:content\u type
,而不是“content-type”
。请参阅下面的最后一个请求,该请求目前正在运行。变量有点乱,但5个小时后,这是我唯一可以工作的电话
@uber_ride = (RestClient::Request.execute(
:method => :post,
:url => "https://sandbox-api.uber.com/v1/requests",
:payload => '{"start_latitude":' + lat_start.to_s + ',"start_longitude":' + lng_start.to_s + ',"end_latitude":' + lat_end.to_s + ',"end_longitude":' + lng_end.to_s + ',"product_id":"' + @uberx_id.to_s + '"}',
:headers => {'Authorization' => 'Bearer ' + session[:request_token], :content_type => 'application/json'}
))
很高兴听到任何关于我如何清理的反馈。:有效载荷=>{'start\u lation'=>lat\u start,'start\u lation'=>lng\u start,'end\u lation'=>lat\u end,'end\u lation'=>lng\u end,'product\u id'=>@uberx\u id}
而不是:data=>{}
应该会有帮助。@mudasobwa。这有助于解决部分问题。我添加了下面的最后一个请求作为答案。很抱歉没有提到引号,我很匆忙地键入了消息。对于字符串,只需使用ruby%{}
内置:%{“开始纬度”:{lat\u开始},…}
。