2017-06-12 98 views
0

我想通过使用Restclient宝石的特定URI传递POST方法请求。但是,我不断从服务器获取400 Bad request。我尝试了多种方式发布数据,并进行了修改。 PFB目前的一个HTTPS:400错误的请求!无效的JSON

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' => '[email protected]', 'dependentName'=> 'Cobb', 'dependentLastName' => 'Cobb', 'lastName' => 'Justin' 
    }, { "Content-Type" => 'application/json'}) 
    puts response 
end 



setup 

我不知道我在这里错过了什么。我试着用get方法使用相同的代码,用于其他api,只用headers,它工作。

请有人让我知道,在我用于POST方法的json中有任何不好的语法。

回答

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

注意to_json

RestClient默认情况下序列化application/x-www-form-urlencoded中的有效载荷。您必须手动序列化您的发布数据。

+0

我以前尝试过添加'to_json',但它没有奏效。仍然不好的请求和无效的JSON :( –

+0

与你目前的代码也是,我收到了同样的东西。我试图重新检查与帖子的人工具和相同的网址与参数在那里工作 –

+0

工程!我想,我留下的空间数据在这里,我尝试删除这些空间,现在它似乎工作:) –