kong API gateway(二):添加API

# 可直接通過dashboard添加
    ---
    http://localhost:8080/#/apis/add

# 也可以通過Linux curl命令添加
參數:
    官方參數說明:https://getkong.org/docs/0.11.x/admin-api/#add-api
    name    // 必填 API名稱
    hosts   // 半可選 指向的域名,多個逗號分割 
    uris    // 半可選 逗號分隔的URI列表
    methods // 半可選 指向的HTTP方法的,多個逗號分隔
    至少有一個'hosts''uris'
    upstream_url // 必填 代理請求地址,https://www.baidu.com
    retries // 可選填 代理失敗重試次數 默認5
    upstream_connect_timeout // 服務連接超時時間 默認60000ms
    upstream_send_timeout // 連續寫入超時時間 默認60000ms
    upstream_read_timeout // 連續請求超時時間 默認60000ms

    ---
    curl -i -X POST \
    --url http://localhost:8001/apis/ \
    --data 'name=example-api' \
    --data 'hosts=example.com' \
    --data 'upstream_url=http://httpbin.org'

# 官文教程
    ---
    https://getkong.org/docs/0.11.x/getting-started/adding-your-api/

# 查看API列表
    ---
    http://localhost:8001/apis
    http://localhost:8080/#/apis

# 查看單個API
    ---
    http://localhost:8001/apis/example-api

# API轉發
    ---
    curl -i -X GET \
    --url http://localhost:8000/ \
    --header 'Host: example.com'

# 注
用php的curl模塊請求返回來的是no API found with those values
https://github.com/postmanlabs/postman-app-support/issues/781(postman)
用postman也是no API found with those values
用Python的pycurl模塊可以訪問的添加的API,用request.get返回的no API found with those values
用Linux的curl命令完全正常
正在找原因,找到在貼出來
    --- php code
    <?php
    $data = ['Host' => 'example.com'];
    $url = 'http://localhost:8000/';
    function phpcurl($url, $data = [])
    {
        // {"message":"no API found with those values"}
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, $data);
        $res = curl_exec($curl);
        curl_close($curl);
        var_export($res);
    }

    function sy()
    {
        // 可以
        $command = "curl -i -X GET   --url http://localhost:8000/   --header 'Host: example.com'";
        var_export(system( $command));
    }

    //phpcurl($url, $data);
    //sy();
    -----------------------------------------------------------------------------
    --- Python code
    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    # author=He

    import pycurl
    import os
    import requests
    headers = {}
    url = 'http://127.0.0.1:8000'
    headers['Host'] = 'example.com'

    #
    # def header_function(header_line):
    #     headers['Host'] = 'example.com'
    #
    # c = pycurl.Curl()
    #
    # c.setopt(c.URL, url)
    # c.setopt(c.HEADERFUNCTION, header_function)
    # c.perform()
    # c.close()
    # command = "curl -i -X GET   --url http://localhost:8000/   --header 'Host: example.com'"
    # r = os.system(command)
    # print(r)

    r = requests.get(url, headers)
    print(r.content.decode())

文章來源:http://blog.csdn.net/qq_26656329/article/details/78293053

發佈了51 篇原創文章 · 獲贊 22 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章