我有一个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)
关于如何改善这一点的任何想法?