How do I send a JSON request in ruby? I have a JSON object but I dont think I can just do .send
. Do I have to have javascript send the form?
Or can I use the net/http class in ruby?
With header - content type = json and body the json object?
How do I send a JSON request in ruby? I have a JSON object but I dont think I can just do .send
. Do I have to have javascript send the form?
Or can I use the net/http class in ruby?
With header - content type = json and body the json object?
I like this light weight http request client called `unirest'
gem install unirest
usage:
It's 2020 - nobody should be using
Net::HTTP
any more and all answers seem to be saying so, use a more high level gem such as Faraday - GithubThat said, what I like to do is a wrapper around the HTTP api call,something that's called like
rv = Transporter::FaradayHttp[url, options]
because this allows me to fake HTTP calls without additional dependencies, ie:
Where the faker looks something like this
I know there are HTTP mocking/stubbing frameworks, but at least when I researched last time they didn't allow me to validate requests efficiently and they were just for HTTP, not for example for raw TCP exchanges, this system allows me to have a unified framework for all API communication.
Assuming you just want to quick&dirty convert a hash to json, send the json to a remote host to test an API and parse response to ruby this is probably fastest way without involving additional gems:
Hopefully this goes without saying, but don't use this in production.
HTTParty makes this a bit easier I think (and works with nested json etc, which didn't seem to work in other examples I've seen.