Last active
January 31, 2020 02:01
-
-
Save mkdika/43fcc28878881d18f4516fecd284555c to your computer and use it in GitHub Desktop.
This file contains 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
# PREREQUIREMENT: | |
# - Ruby 2.0.0 or higher | |
# - Install Gems: | |
# - `gem install httparty` | |
require 'HTTParty' | |
require 'json' | |
GOREST_ACCESS_TOKEN = 'abc123' | |
def beautify_json(str) | |
JSON.pretty_generate(JSON.parse(str)) | |
end | |
# GET | |
url1 = 'https://www.metaweather.com/api/location/search/?query=jakarta' | |
response1 = HTTParty.get(url1) | |
puts "GET - Metaweather" | |
puts "url : #{url1}" | |
puts "code : #{response1.code} (#{response1.message})" | |
puts "headers :\n#{response1.headers}" | |
puts "body :\n#{beautify_json response1.body}" | |
puts "title : #{response1.to_a[0]['title']}" | |
puts "location: #{response1.to_a[0]['location_type']}" | |
puts "xyz : #{response1.to_a[0]['xyz']}" | |
puts '' | |
# GET - Metaweather | |
# url : https://www.metaweather.com/api/location/search/?query=jakarta | |
# code : 200 (OK) | |
# headers : | |
# {"x-xss-protection"=>["1; mode=block"], "content-language"=>["en"], "x-content-type-options"=>["nosniff"], "strict-transport-security"=>["max-age=2592000; includeSubDomains"], "vary"=>["Accept-Language, Cookie"], "allow"=>["GET, HEAD, OPTIONS"], "x-frame-options"=>["DENY"], "content-type"=>["application/json"], "x-cloud-trace-context"=>["5af4f71128ea90dbede1b344a0a3b10f;o=1"], "date"=>["Fri, 31 Jan 2020 01:59:44 GMT"], "server"=>["Google Frontend"], "content-length"=>["95"], "connection"=>["close"]} | |
# body : | |
# [ | |
# { | |
# "title": "Jakarta", | |
# "location_type": "City", | |
# "woeid": 1047378, | |
# "latt_long": "-6.171440,106.827820" | |
# } | |
# ] | |
# title : Jakarta | |
# location: City | |
# xyz : | |
# POST | |
url2 = 'https://gorest.co.in/public-api/users' | |
headers = { | |
'Accept' => 'application/json', | |
'Content-Type' => 'application/json', | |
'Authorization' => "Bearer #{GOREST_ACCESS_TOKEN}" | |
} | |
body = { | |
'first_name' => 'Maikel', | |
'last_name' => 'Chandika', | |
'gender' => 'male', | |
'email' => '[email protected]', | |
'status' => 'active' | |
} | |
response2 = HTTParty.post(url2, headers: headers, body: body, timeout: 3) | |
puts "POST - User" | |
puts "url : #{url2}" | |
puts "code : #{response2.code} (#{response2.message})" | |
puts "headers :\n#{response2.headers}" | |
puts "body :\n#{beautify_json response2.body}" | |
puts '' | |
# POST - User | |
# url : https://gorest.co.in/public-api/users | |
# code : 200 (OK) | |
# headers : | |
# {"server"=>["nginx"], "date"=>["Fri, 31 Jan 2020 01:59:46 GMT"], "content-type"=>["application/json; charset=UTF-8"], "transfer-encoding"=>["chunked"], "connection"=>["close"], "vary"=>["Accept-Encoding", "Accept"], "www-authenticate"=>["Bearer realm=\"api\""]} | |
# body : | |
# { | |
# "_meta": { | |
# "success": false, | |
# "code": 401, | |
# "message": "Authentication failed." | |
# }, | |
# "result": { | |
# "name": "Unauthorized", | |
# "message": "Your request was made with invalid credentials.", | |
# "code": 0, | |
# "status": 401 | |
# } | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment