> ## Documentation Index
> Fetch the complete documentation index at: https://alltick.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# HTTP リクエスト例

> 本ページでは、AllTick REST APIのHTTPリクエスト例コードを提供します。Go、Python、Java、PHPなど主要言語を対象に、リクエストURLの構築、tokenとqueryパラメータの設定、リクエスト送信、レスポンス解析の方法を示し、開発者が為替、株式、暗号資産などのリアルタイムTickデータAPIへ迅速にHTTP接続・デバッグできるよう支援します。

<CodeGroup>
  ```go Go theme={null}
  package main

  import (
  	"fmt"
  	"io/ioutil"
  	"log"
  	"net/http"
  )

  func http_example() {

  	/*
  		次の JSON を URL エンコードし、HTTP クエリ文字列の query フィールドに設定します
  		{"trace" : "go_http_test1","data" : {"code" : "700.HK","kline_type" : 1,"kline_timestamp_end" : 0,"query_kline_num" : 2,"adjust_type": 0}}

  		注意：
  		github: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
  		Token 申請：https://alltick.co
  		下記 URL の testtoken をご自身の Token に置き換えてください
  		外国為替、暗号通貨、貴金属の API アドレス：
  		https://quote.alltick.co/quote-b-api
  		株式 API アドレス：
  		https://quote.alltick.co/quote-stock-b-api
  	*/
  	url := "https://quote.alltick.co/quote-stock-b-api/kline"
  	log.Println("リクエスト URL：", url)

  	req, err := http.NewRequest("GET", url, nil)
  	if err != nil {
  		fmt.Println("Error creating request:", err)
  		return
  	}

  	q := req.URL.Query()
  	token := "testtoken"
  	q.Add("token", token)
  	queryStr := `{"trace":"1111111111111111111111111","data":{"code":"AAPL.US","kline_type":1,"kline_timestamp_end":0,"query_kline_num":10,"adjust_type":0}}`
  	q.Add("query", queryStr)
  	req.URL.RawQuery = q.Encode()
  	// リクエストを送信
  	resp, err := http.DefaultClient.Do(req)
  	if err != nil {
  		fmt.Println("Error sending request:", err)
  		return
  	}
  	defer resp.Body.Close()

  	body2, err := ioutil.ReadAll(resp.Body)

  	if err != nil {

  		log.Println("レスポンスの読み取りに失敗：", err)

  		return

  	}

  	log.Println("レスポンス内容：", string(body2))

  }
  ```

  ```java Java theme={null}
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;

  public class HttpJavaExample {

      public static void main(String[] args) {

          try {

              /*
              次の JSON を URL 形式にエンコードし、HTTP リクエストの query フィールドにコピーします
              {"trace" : "java_http_test1","data" : {"code" : "700.HK","kline_type" : 1,"kline_timestamp_end" : 0,"query_kline_num" : 2,"adjust_type": 0}}
              特別な注意:
              GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
              Token 申請: https://alltick.co
              下記 URL の "testtoken" をご自身の token に置き換えてください
              外国為替、暗号資産、貴金属の API アドレス:
              https://quote.alltick.co/quote-b-api
              株式 API アドレス:
              https://quote.alltick.co/quote-stock-b-api
              */
              String url = "http://quote.alltick.co/quote-stock-b-api/kline?token=testtoken&query="
                      + "%7B%22trace%22%20%3A%20%22java_http_test1%22%2C%22data%22%20%3A%20%7B"
                      + "%22code%22%20%3A%20%22700.HK%22%2C%22kline_type%22%20%3A%201%2C"
                      + "%22kline_timestamp_end%22%20%3A%200%2C%22query_kline_num%22%20%3A%202%2C"
                      + "%22adjust_type%22%3A%200%7D%7D";

              URL obj = new URL(url);

              HttpURLConnection con = (HttpURLConnection) obj.openConnection();

              con.setRequestMethod("GET");

              int responseCode = con.getResponseCode();

              System.out.println("Response Code: " + responseCode);

              BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

              String inputLine;

              StringBuffer response = new StringBuffer();

              while ((inputLine = in.readLine()) != null) {
                  response.append(inputLine);
              }

              in.close();

              System.out.println(response.toString());

          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  ```

  ```php PHP theme={null}
  <?php

  // 特別な注意:
  // GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
  // Token 申請: https://alltick.co
  // 下記 URL の "testtoken" をご自身の token に置き換えてください
  // 外国為替、暗号資産、貴金属の API アドレス:
  // https://quote.alltick.co/quote-b-ws-api
  // 株式 API アドレス:
  // https://quote.alltick.co/quote-stock-b-ws-api

  $params = '{"trace":"1111111111111111111111111","data":{"code":"AAPL.US","kline_type":1,"kline_timestamp_end":0,"query_kline_num":10,"adjust_type":0}}';

  $url = 'https://quote.alltick.co/quote-stock-b-api/kline?token=testtoken';
  $method = 'GET';

  $opts = array(CURLOPT_TIMEOUT => 10, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false);

  /* リクエストタイプに応じて具体的なパラメータを設定 */
  switch (strtoupper($method)) {
      case 'GET':
          $opts[CURLOPT_URL] = $url.'&query='.rawurlencode($params);
          $opts[CURLOPT_CUSTOMREQUEST] = 'GET';
          break;
      default:
  }

  /* curl リクエストを初期化して実行 */
  $ch = curl_init();
  curl_setopt_array($ch, $opts);
  $data = curl_exec($ch);
  $error = curl_error($ch);
  curl_close($ch);

  if ($error) {
      $data = null;
  }

  echo $data;
  ?>
  ```

  ```python Pyton theme={null}
  import time
  import requests
  import json

  # Extra headers
  test_headers = {
      'Content-Type': 'application/json'
  }

  '''
  # 特別な注意:
  # GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
  # Token 申請: https://alltick.co
  # 下記 URL の "testtoken" をご自身の token に置き換えてください
  # 外国為替、暗号資産、貴金属の API アドレス:
  # https://quote.alltick.co/quote-b-ws-api
  # 株式 API アドレス:
  # https://quote.alltick.co/quote-stock-b-ws-api
  次の JSON をエンコードし、HTTP クエリ文字列の "query" フィールドにコピーします
  {"trace": "python_http_test1", "data": {"code": "700.HK", "kline_type": 1, "kline_timestamp_end": 0, "query_kline_num": 2, "adjust_type": 0}}
  {"trace": "python_http_test2", "data": {"symbol_list": [{"code": "700.HK"}, {"code": "UNH.US"}]}}
  {"trace": "python_http_test3", "data": {"symbol_list": [{"code": "700.HK"}, {"code": "UNH.US"}]}}
  '''
  test_url1 = (
      'https://quote.alltick.co/quote-stock-b-api/kline?token=testtoken&query='
      '%7B%22trace%22%20%3A%20%22python_http_test1%22%2C%22data%22%20%3A%20%7B'
      '%22code%22%20%3A%20%22700.HK%22%2C%22kline_type%22%20%3A%201%2C'
      '%22kline_timestamp_end%22%20%3A%200%2C%22query_kline_num%22%20%3A%202%2C'
      '%22adjust_type%22%3A%200%7D%7D'
  )
  test_url2 = (
      'https://quote.alltick.co/quote-stock-b-api/depth-tick?token=testtoken&query='
      '%7B%22trace%22%20%3A%20%22python_http_test2%22%2C%22data%22%20%3A%20%7B'
      '%22symbol_list%22%3A%20%5B%7B%22code%22%3A%20%22700.HK%22%7D%2C'
      '%7B%22code%22%3A%20%22UNH.US%22%7D%5D%7D%7D'
  )
  test_url3 = (
      'https://quote.alltick.co/quote-stock-b-api/trade-tick?token=testtoken&query='
      '%7B%22trace%22%20%3A%20%22python_http_test3%22%2C%22data%22%20%3A%20%7B'
      '%22symbol_list%22%3A%20%5B%7B%22code%22%3A%20%22700.HK%22%7D%2C'
      '%7B%22code%22%3A%20%22UNH.US%22%7D%5D%7D%7D'
  )

  resp1 = requests.get(url=test_url1, headers=test_headers)
  time.sleep(1)
  resp2 = requests.get(url=test_url2, headers=test_headers)
  time.sleep(1)
  resp3 = requests.get(url=test_url3, headers=test_headers)

  # リクエストから返されたデコード済みテキスト
  text1 = resp1.text
  print(text1)

  text2 = resp2.text
  print(text2)

  text3 = resp3.text
  print(text3)
  ```
</CodeGroup>

***

#### AllTick 公式サイト

<Note>
  公式サイト：<a href="https://alltick.co/">[https://alltick.co/](https://alltick.co/)</a>
</Note>

<script src="/seo-keywords.js" />
