错误在rails中发送POST和HTTPARTY语义错误您的请求

问题描述 投票:0回答:1

这是我的错误,当我使用我的方法{“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
ruby-on-rails json ruby hash
1个回答
0
投票

问题是由to_json在错误的地方调用引起的。

整个请求体应该作为JSON发送。在您的代码中,您调用to_json以获取稍后被推入@body["users"]数组的哈希。

请从to_json中删除send_venue_notification,并在发送请求时将其命名为@body

HTTParty.post("http://api.treble.ai/api/poll/49/deploy", headers: @headers, body: @body.to_json)
© www.soinside.com 2019 - 2024. All rights reserved.