天天看点

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

继续阅读