HTTPS:400错误的请求!无效的JSON

时间:2017-06-12 07:46:46

标签: json ruby rest api rest-client

我正在尝试使用Restclient gem传递针对特定URI的POST方法请求。但是,我不断从服务器获取400 Bad request。我已尝试过多种方式发布数据,并进行了修改。 PFB是当前的一个

require 'minitest'
require 'rest-client'
require 'json'
require 'pry'
require 'uri/https'


#class APITest < Minitest::Test
def setup
  response = RestClient.post("", {'userType' => 'nonsso', 'firstName' => 'Justin9', 'isDependentMajor' => true, 'email' => 'randomemail0053@gmail.com', 'dependentName'=> 'Cobb', 'dependentLastName' => 'Cobb', 'lastName' => 'Justin'
  }, { "Content-Type" => 'application/json'})
  puts response
end



setup

我无法理解我在这里失踪的是什么。我尝试使用相同的代码,对于其他api,使用get方法,只使用headers并且它可以工作。

请有人告诉我,json中的任何错误语法都用于POST方法。

1 个答案:

答案 0 :(得分:3)

 response = RestClient.post("", {'userType' => 'nonsso', 'firstName' => 'Justin9', 'isDependentMajor' => true, 'email' => 'randomemail0053@gmail.com', 'dependentName'=> 'Cobb', 'dependentLastName' => 'Cobb', 'lastName' => 'Justin'
  }.to_json, { "Content-Type" => 'application/json'})

请注意to_json

RestClient默认情况下会在application/x-www-form-urlencoded中序列化有效负载。您必须手动序列化您的帖子数据。