To make an HTTP request with a custom body as JSON in Ruby, you can use the built-in Net::HTTP
library along with the json
library to encode your data as JSON. Here's an example code snippet:
main.rb446 chars18 lines
In this example, we create a Net::HTTP::Post
object with the URI of our endpoint and set the Content-Type
header to application/json
to indicate that we are sending JSON data. We then encode our custom data as JSON using to_json
and set it as the body of the request.
We send the request using Net::HTTP.start
with the hostname, port, and SSL settings of our URI, and handle the response as desired (in this case, simply printing the response body).
gistlibby LogSnag