Skip to content

Latest commit

 

History

History

api_path_params

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Shows how to create an API to handle path parameters using OpenWhisk

Let's say you want a web API in the form of https://hostname/api/v1/constacts/:id

The OpenWhisk Action shows an example on how to handle a http request with GET and path /constacts/:id

const UrlPattern = require('url-pattern');
const pattern = new UrlPattern('/api/v1/contacts/:id');
function main({__ow_path:path}) {
  return { body: findContactById(pattern.match(path)) }
}

Install dependencies

  1. Install and Configure CLI bx wsk using IBM Cloud CLI Functions plugin
    bx wsk list
    
  2. Install nodejs deps:
    npm install
    

TLDR;

npm start

Build

npm run build

Deploy

npm run deploy

Build and Deploy

npm run all

Get API URL

npm run api

API info display including URL

Action: /[email protected]_demo/getContact
   API Name: contacts
   Base path: /api/v1
   Path: /contacts/{id}
   Verb: get
   URL: https://<apigatewayhost>/<tenantid>/api/v1/contacts/{id}

Call API

Open a browser, or use tool of choice using the URL from previous step

curl https://<apigatewayhost>/<tenantid>/api/v1/contacts/1234

Output: Found contact from data store with id 1234 return resource with content-type application/json and statusCode 200

{
  "firstName": "Carlos",
  "lastName": "Santana",
  "twitter": "csantanapr",
  "id": "1234"
}

Bonus Points

Using IBM Cloud API Management you can map the URL to your cutom domain. For example uploading a SSL certificate generated using something like Let's Encrypt and mapping the API to your custom domain name. Then you can have the API endpoint on your domain (i.e. https://example.com//api/v1/contacts/{id})