Step 2. Create and Test API Service

Last updated: 2020-09-01 10:01:49

    Overview

    In this part, you will create a service in API Gateway and related API rules, connect them to the SCF function created in Step 1, and test the correctness of the API through the API test in the console.

    Note:

    The API service and function must be in the same region. In this tutorial, the Beijing region is used to create the API service.

    Creating API Service and Rules

    1. Log in to the Tencent Cloud Console, select Tencent Cloud Services > Middleware > API Gateway at the top of the page, and enter the "API Gateway" overview page.

    2. Select Service on the left sidebar and switch the region to Beijing at the top of the "Service" page.

    3. On the "Service" page, click Create to create an API service.

    4. In the "Create Service" pop-up window, enter blogAPI as the service name and click Submit.

    5. After completing the service creation, enter the created blogAPI service and select the Manage API tab.

    6. Click New to enter the "Create API" page. In the "Frontend Configuration" step, refer to the following main parameters to create the API:

      • API Name: custom API name.
      • Path: /article.
      • Request Method: GET.
      • Authentication Type: to facilitate subsequent testing, select No authentication.
        Keep the remaining options as default and click Next.
    7. In the "Backend Configuration" step, refer to the following main parameters to create the API:

      • Backend Type: select cloud function.
      • Cloud Function: select the blogArticle function created in Step 1.
        Keep the remaining options as default and click Next.
    8. Click Complete in "Response Result" to complete the API creation.

    9. Click Create on the Manage API tab again to create another API with the path of /article/{articleId} and the request method of GET. Select No authentication, enter the parameter articleId in the parameter configuration with Path as the parameter location, int as the parameter type, and 1 as the default value, and click Next.

    10. Select Cloud Function as the backend type, select the blogArticle function created in Step 1 as the function, and click Next.

    11. Click Complete in "Response Result" to complete the API creation.

    Debugging API Rules

    1. To debug the API /article created in step 6 above, click API Debug, send a request on the debugging page, and check whether the response body in the returned result is shown as follows:
      [{"category": "blog", "time": "2017-12-05 13:45", "id": 1, "title": "hello world"}, {"category": "blog", "time": "2017-12-06 08:22", "id": 2, "title": "record info"}, {"category": "python", "time": "2017-12-06 18:32", "id": 3, "title": "python study"}]
    2. To debug the API /article/{articleId} created in step 9 above, click API Debug, modify the request parameter value to 1, send a request on the debugging page, and check whether the response body in the returned result is shown as follows:
      {"category": "blog", "content": "first blog! hello world!", "time": "2017-12-05 13:45", "id": 1, "title": "hello world"}
    3. You can also modify the value of the request parameter articleId in step 2 to another number and check the response content.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help