I have to make this request in rails 6:
curl --location --request POST 'https://www.example.com/auth' \
--header 'Content-Type: application/json' \
--data-raw '{
"Username": "my_username",
"Password": "my_password"
}'
We usually use HTTParty to make http requests, but i faced some problems trying to pass raw data into the request. I've already tried:
url = 'https://www.example.com/auth'
auth_data = { Username: 'my_username', Password: 'my_password' }
headers = {'Content-Type' => 'application/json'}
HTTParty.post(url, data: auth_data, headers: headers)
HTTParty.post(url, data: auth_data.to_json, headers: headers)
HTTParty.post(url, data: [auth_data].to_json, headers: headers)
HTTParty.post(url, body: auth_data, headers: headers)
And in all cases, the response says that no data was passed
Eventually, this worked for me:
uri = URI.parse('https://www.example.com/auth')
auth_data = { Username: 'my_username', Password: 'my_password' }
headers = {'Content-Type' => 'application/json'}
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.post(uri.path, auth_data.to_json, headers)
Update 2022: Now I realize there was a simpler way:
HTTParty.post(url, body: auth_data.to_json, headers: headers)