Search code examples
jsonapicurlkong

How do i invoke my api which is configured in Kong through web browser?


I have created a new API configuration through kong..(dummy API) as below.

curl -i -X POST \

--url http://localhost:8001/apis/ \ --data 'name=myRESTapi' \ --data 'hosts=myrestapi.com' \ --data 'upstream_url=http://demo1592110.mockable.io/hello'

HTTP/1.1 201 Created Date: Fri, 08 Dec 2017 09:25:35 GMT Content-Type: application/json; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Access-Control-Allow-Origin: * Server: kong/0.11.2

{"created_at":1512725135862,"strip_uri":true,"id":"d066b086-6d64-45b7-9908-f6411c456ce4","hosts":["myrestapi.com"],"name":"myRESTapi","http_if_terminated":false,"preserve_host":false,"upstream_url":"http://demo1592110.mockable.io/hello","upstream_connect_timeout":60000,"upstream_send_timeout":60000,"upstream_read_timeout":60000,"retries":5,"https_only":false} [ec2-user@ip-172-31-10-102 ~]$

I am invoking that API through curl command in linux instance as below

curl -i -X GET \

--url http://localhost:8000/ \ --header 'Host: myrestapi.com'

HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 49 Connection: keep-alive access-control-allow-origin: * X-Cloud-Trace-Context: 7806a9e612e31e610f9278b98371a60e Date: Fri, 08 Dec 2017 09:26:55 GMT Server: Google Frontend X-Kong-Upstream-Latency: 227 X-Kong-Proxy-Latency: 46 Via: kong/0.11.2

{ "msg": "shruthi mock API" }

i want to know how to invoke this api through web browser which should pass through kong in another machine.

i can directly execute through upstream url in browser : http://demo1592110.mockable.io/hello

but i want to hit kong and inturn want to invoke my api, but not using curl command, but directly want to achieve this through web browser.

i am new to kong, not sure if this is valid or not. Any help is appreciated.


Solution

  • With "hosts" approach you need to pass the host in the header value which you cannot replicate from browser directly. For hosts approach you need to use any rest client to pass header value or programatically achieve it.

    But as you mentioned you need to try it using browser directly, then I suggest you to use the "uris" when adding api to kong.

    curl -i -X POST \
    --url http://localhost:8001/apis/ \ 
    --data "name=myRESTapi" \ 
    --data "uris=/hello" \ 
    --data "upstream_url=http://demo1592110.mockable.io"
    

    You can see 'uris' added and URI from upstream_url is removed. No need of hosts after that.

    Now if you hit the url in browser as

    http://localhost:8000/hello
    

    You can achieve the same.