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

To make an HTTP request with a custom body as HTML in Ruby, you can use the net/http library. Here's an example code snippet that creates a POST request with a custom HTML body and sends it to a specified URL:

main.rb
require 'net/http'

url = URI("http://example.com/path/to/resource")

request = Net::HTTP::Post.new(url)
request['Content-Type'] = 'text/html'
request.body = '<html><body><h1>Hello world!</h1></body></html>'

response = Net::HTTP.start(url.hostname, url.port) do |http|
  http.request(request)
end

puts response.body
318 chars
14 lines

In this example, we first create a URI object representing the URL we want to send the request to. We then create a new Net::HTTP::Post object and set its Content-Type header to 'text/html', indicating that the body of the request is HTML.

We then set the body of the request to the custom HTML code using the body method. Finally, we use the start method of Net::HTTP to send the request and receive the response, which we print to the console with puts.

Note that you may need to replace example.com, /path/to/resource, and the custom HTML body in this code with your own values, depending on the specific URL you're working with and the HTML you want to send.

gistlibby LogSnag