Skip to content

Instantly share code, notes, and snippets.

@mkdika
Last active January 31, 2020 02:01
Show Gist options
  • Save mkdika/43fcc28878881d18f4516fecd284555c to your computer and use it in GitHub Desktop.
Save mkdika/43fcc28878881d18f4516fecd284555c to your computer and use it in GitHub Desktop.

Revisions

  1. mkdika revised this gist Jan 31, 2020. 1 changed file with 23 additions and 18 deletions.
    41 changes: 23 additions & 18 deletions httparty_demo.rb
    Original file line number Diff line number Diff line change
    @@ -20,8 +20,29 @@ def beautify_json(str)
    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 = {
    @@ -44,27 +65,11 @@ def beautify_json(str)
    puts "body :\n#{beautify_json response2.body}"
    puts ''

    # RESULT:
    # 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"=>["918437023a18c90790f41c79d6de3b2f"], "date"=>["Thu, 30 Jan 2020 15:59:04 GMT"], "server"=>["Google Frontend"], "content-length"=>["95"], "connection"=>["close"]}
    # body :
    # [
    # {
    # "title": "Jakarta",
    # "location_type": "City",
    # "woeid": 1047378,
    # "latt_long": "-6.171440,106.827820"
    # }
    # ]

    # POST - User
    # url : https://gorest.co.in/public-api/users
    # code : 200 (OK)
    # headers :
    # {"server"=>["nginx"], "date"=>["Thu, 30 Jan 2020 15:59:06 GMT"], "content-type"=>["application/json; charset=UTF-8"], "transfer-encoding"=>["chunked"], "connection"=>["close"], "vary"=>["Accept-Encoding", "Accept"], "www-authenticate"=>["Bearer realm=\"api\""]}
    # {"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": {
    @@ -78,4 +83,4 @@ def beautify_json(str)
    # "code": 0,
    # "status": 401
    # }
    # }
    # }
  2. mkdika created this gist Jan 30, 2020.
    81 changes: 81 additions & 0 deletions httparty_demo.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    # 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 ''

    # 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 ''

    # RESULT:
    # 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"=>["918437023a18c90790f41c79d6de3b2f"], "date"=>["Thu, 30 Jan 2020 15:59:04 GMT"], "server"=>["Google Frontend"], "content-length"=>["95"], "connection"=>["close"]}
    # body :
    # [
    # {
    # "title": "Jakarta",
    # "location_type": "City",
    # "woeid": 1047378,
    # "latt_long": "-6.171440,106.827820"
    # }
    # ]

    # POST - User
    # url : https://gorest.co.in/public-api/users
    # code : 200 (OK)
    # headers :
    # {"server"=>["nginx"], "date"=>["Thu, 30 Jan 2020 15:59:06 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
    # }
    # }