Last active
January 23, 2018 00:11
-
-
Save Qata/ec01adffddb8d10b215e92a63eddf1df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'yaml' | |
require 'json' | |
# You can either pipe in the data that this script expects or | |
# The data expected by this script should be in the following format: | |
# Request: | |
# Method: GET | |
# URL: https://example.com/example/url/slug | |
# Header: | |
# Content-Type: application/x-www-form-urlencoded | |
# User-Agent: Example User Agent | |
# Body: {"sample": "json"} / key=value&key2=value2 / null | |
if $stdin.tty? | |
puts "Paste in the request YAML then press ESC and RETURN" | |
yaml = YAML.load(gets("\e").chomp("\e")) | |
else | |
yaml = YAML.load(ARGF.readlines.join) | |
end | |
request = yaml['Request'] | |
method = "-X #{request['Method']}" | |
headers = request['Header'].map { |key, value| "-H '#{key}: #{value}'" }.join(" ") | |
url = request['URL'] | |
body = request['Body'] | |
if body.is_a?(Hash) | |
data = "-d #{JSON.generate(body)}" | |
elsif body.is_a?(String) | |
data = body.split('&').map { |value| "-F '#{value}'"}.join(" ") | |
else | |
data = "" | |
end | |
command = "curl #{method} #{headers} #{data} #{url}" | |
puts command |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment