Emjey
Emjey

Reputation: 2063

HTTPS: 400 Bad request! Invalid JSON

I am trying to pass a POST method request for a specific URI using the Restclient gem. I am however, continously getting 400 Bad request from the server. I have tried numerous ways of posting the data, with modifications. PFB the current one

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

I am at a loss to understand what am I missing here. I tried using the same code, for an other api, with get method, only with headers and it works.

Please can someone let me know, any bad syntax in json I am using for the POST method.

Upvotes: 0

Views: 6477

Answers (1)

Aetherus
Aetherus

Reputation: 8888

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

Note the to_json.

RestClient serializes the payload in application/x-www-form-urlencoded by default. You have to manually serialize your post data.

Upvotes: 3

Related Questions