[英]Error SENDING POST in rails and HTTPPARTY semantic error my request
這是我的錯誤,當我使用我的方法{“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
問題是由to_json
在錯誤的地方調用引起的。
整個請求正文應作為JSON發送。 在您的代碼中,您調用to_json
以獲取稍后被推送到@body["users"]
數組的哈希。
請在發送請求時從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.