You can use the Net::HTTP
library in Ruby to send a post request to an endpoint with basic authentication. Here's an example method:
main.rb338 chars12 lines
Explanation of the method:
url
: URL of the endpoint to send the post request todata
: A hash of the data to send in the post requestusername
: Username for basic authenticationpassword
: Password for basic authenticationThe Net::HTTP
library is used to create a connection to the endpoint. A Net::HTTP::Post
object is created with the URI of the endpoint. basic_auth
is called on the request to add the username and password for basic authentication. The set_form_data
method is used to add the data to the post request. The request is then sent using http.request(request)
and the response code and body are returned.
Example usage:
main.rb232 chars4 lines
gistlibby LogSnag