LoginSignup
0
1

More than 1 year has passed since last update.

メモ: WebAPIを使ってみる

Last updated at Posted at 2017-02-25

サンプル

参考

jQuery

スクリーンショット_2017-02-25_12-19-32.png

search_address.html
郵便番号
<input id="zip" type="text" size="10" maxlength=7 onKeyUp='search_zip();' />
<input id="search" type="button" value="検索" />

<div id="responses"></div>

<script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
function search_zip() {
    if ($("#zip").val().length != 7) {
	    return false;
    }

    $.getJSON('http://zipcloud.ibsnet.co.jp/api/search?callback=?', {
        zipcode: $('#zip').val()
    })

    .done(function(data) {

      if (data.results) {
        var result = data.results[0];
        $('#responses').html(
            result.address1 + result.address2 + result.address3
        );
      } else {
        $('#responses').html('該当する住所が存在しません。');
      }

    });
}

</script>

jQuery-template

_address.html
<span data-content="address1"></span>
<span data-content="address2"></span>
<span data-content="address3"></span>
index.html
郵便番号
<input id="zip" type="text" size="10" maxlength=7 onChange='$("#search").click();' />
<input id="search" type="button" value="検索" />

<div id="area_address"></div>

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="jquery.loadTemplate.min.js"></script>
<script>
$(function() {
  $('#search').click(function() {

    $.getJSON('http://zipcloud.ibsnet.co.jp/api/search?callback=?', {
        zipcode: $('#zip').val()
    })

    .done(function(json) {

    $("#area_address").loadTemplate(
      "_address.html",
      {
        "address1" : json.results[0].address1,
        "address2" : json.results[0].address2,
        "address3" : json.results[0].address3,
    });

    });

  });
});
</script>

メモ: curl

# GET
curl -H GET 'http://localhost:3000/XXXX/XXXX?name=hello&id=100' \
  -H 'Content-Type:application/json;charset=utf-8' \
  -H 'Authorization: Bearer XXXX' \
  | jq .

# POST
curl -H POST 'http://localhost:3000/XXXX/XXXX' \
  -H 'Content-Type: application/json;charset=utf-8' \
  -d '{"name":"hello", "id":"100"}' \
  -H 'Authorization: Bearer XXXX' \
  | jq .

メモ: php

<?php
$options  = array (
  'http' => 
  array (
    'ignore_errors' => true,
    'header' => 
    array (
      0 => 'authorization: Bearer YOUR_API_TOKEN',
    ),
  ),
);
 
$context  = stream_context_create( $options );
$response = file_get_contents(
    'https://public-api.wordpress.com/rest/v1/me/',
    false,
    $context
);
$response = json_decode( $response );

echo $response->message;

メモ: python

import json
import requests

url = 'http://hoge.com'

data = {
        'hoge': 'fuga',
        'fuga' : {'fuga1': 'いいね', 'fuga2': 'だめよ'}
        }

headers = {'Content-Type': 'application/json'}
requests.post(url,data=json.dumps(data), headers=headers)

メモ: ruby

require 'net/http'

Net::HTTP.get_print 'hoge.com', '/some/param'
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1