make a http request with a custom body as text in ruby

To make an HTTP request with a custom body as text in Ruby, we can use the Net::HTTP library. Here's an example:

main.rb
require 'net/http'

uri = URI('http://example.com/api')
body = 'This is the body of the request.'

response = Net::HTTP.post(uri, body, {'Content-Type' => 'text/plain'})

puts response.body
190 chars
9 lines

In this example, we're making a POST request to http://example.com/api with the body of the request set to 'This is the body of the request.' and the Content-Type header set to text/plain. We're then printing out the response body to the console.

You can modify this example to use different HTTP methods (GET, PUT, DELETE, etc.) and different content types as needed.

gistlibby LogSnag