Skip to content

Instantly share code, notes, and snippets.

@kimat
Forked from justinwoo/requesttest.rb
Created June 16, 2023 13:47
Show Gist options
  • Save kimat/6a94ddec0569a8226a2f97b6f250b03e to your computer and use it in GitHub Desktop.
Save kimat/6a94ddec0569a8226a2f97b6f250b03e to your computer and use it in GitHub Desktop.
simple HTTP request testing using net/http and a JSON POST payload | Update: I guess I might start updating this again. I am pretty fickle.
#! usr/bin/ruby
require 'uri'
require 'net/http'
require 'json'
def getRequest(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request["Accept"] = "application/json"
return http.request(request)
end
def deleteRequest(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Delete.new(uri.request_uri)
request["Accept"] = "application/json"
return http.request(request)
end
def putRequest(url, payload)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Put.new(uri.request_uri)
request["Accept"] = "application/json"
request.content_type = "application/json"
request.body = payload
return http.request(request)
end
def postRequest(url, payload)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request["Accept"] = "application/json"
request.content_type = "application/json"
request.body = payload
return http.request(request)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment