[英]Error SENDING POST in rails and HTTPPARTY semantic error my request
this is my error when I lanunched my method {"errors"=>{"users"=>["Missing data for required field."]}, "msg"=>"The request was well-formed but was unable to be followed due to semantic errors."} 这是我的错误,当我使用我的方法{“errors”=> {“users”=> [“缺少所需字段的数据。”]},“msg”=>“请求格式正确但无法成为我的错误由于语义错误而被紧随其后。”}
class BookingNotifier
include HTTParty
def initialize(booking_id)
@booking = Booking.find booking_id
@venue = @booking.event.service.venue
@body = { "users" => [] }
@headers = {
"Accept" => "application/json",
"Authorization" => "ENV_KEY",
"Content-Type" => "application/json"
}
end
def send_venue_notification
venues_objects = []
if @venue.notifications_enabled
venues_objects << { "cellphone" => @booking.event.service.venue.phone,
"country_code" => "+57",
"user_session_keys" => [{ "key" => "Nombre", "value" => @booking.profile.name },
{ "key" => "Centro", "value" => @booking.event.service.venue.name },
{ "key" => "Cupos", "value" => @booking.quantity },
{ "key" => "Horarios", "value" => @booking.time.strftime("%I:%M %p el %d/%m/%Y") }] }.to_json
@body["users"] = venues_objects
make_request_venue
end
end
def make_request_venue
HTTParty.post("http://api.treble.ai/api/poll/49/deploy", headers: @header, body: @body)
end
The problem is caused by to_json
called in the wrong place. 问题是由to_json
在错误的地方调用引起的。
The whole request body should be sent as a JSON. 整个请求正文应作为JSON发送。 In your code, you call to_json
for a hash that is later pushed into @body["users"]
array. 在您的代码中,您调用to_json
以获取稍后被推送到@body["users"]
数组的哈希。
Please remove to_json
from send_venue_notification
and call it for the @body
when sending the request: 请在发送请求时从send_venue_notification
删除to_json
并为@body
调用它:
HTTParty.post("http://api.treble.ai/api/poll/49/deploy", headers: @headers, body: @body.to_json)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.