API不接受将ruby哈希转换为JSON

时间:2019-04-11 12:09:03

标签: ruby-on-rails json

我有一个ruby哈希,我需要以JSON的形式提供给API,但不会被接受并且会引发错误。

params = {"eventIds" =>
    ["client.created", "client.updated", "client.deactivated"], 
    "eventSchemaVersion"=>1
}

当我致电params.to_json时,我得到:

"{\"eventIds\":[\"client.created\",\"client.updated\",\"client.deactivated\"],\"eventSchemaVersion\":1"}"

,当我将其作为请求正文提供时,出现400错误。 api指示提供以下内容:

request.body = "{requestBody}"

{
  "eventIds" : [
    "classSchedule.created",
    "classSchedule.updated",
    "classSchedule.cancelled"
  ],
  "eventSchemaVersion" : 1
}

我只是在请求中加入了request.body = params.to_json

编辑: 这是我的完整要求:

require 'uri'
require 'net/http'

url = URI("https://mb-api.mindbodyonline.com/push/api/v1/subscriptions")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["api-key"] = ENV['API_KEY']
request["content-type"] = 'application/json'

params = {"eventIds" =>
           ["client.created", "client.updated", "client.deactivated"], 
           "eventSchemaVersion"=>1
         }
request.body = params.to_json

response = http.request(request)

关于如何改善这一点的任何想法?

0 个答案:

没有答案