POST/PUT call with Path Parameter and a Request Body


#1

@restful(subPath=’…/update/{id}’)
updateEmployee: function(id: string, employee: json): json js server

I am working on an POST API call. I need the ‘id’ as a path variable and ‘employee’ as a request body. I am able to read ‘employee’ json object in the function when it is a POST call but ‘id’ shows up as null. I am able to read ‘id’ string in the function when it is a PUT call but ‘employee’ shows up as null.

If I can include ‘id’ field in the request body, I am able to get the ‘id’. I am working on how to get the ‘id’ from path variable.

I am using postman to make these calls, and also included Content-Type = “application/json”.

Any thoughts on how to solve this issue? Please let me know. Thank you.


#2

@restful is for GET requests, you should be able to have id in the path and employee as a query parameter. An example of using this annotation is as follows:
Api.c3typ:

@restful(endpoint='topology')
type Api {
    @restful(subPath='sites/{siteId}/buildings', paramsArg='params')
    fetchSite: function(siteId: string, params: ApiSpec): json js all
}

Api.js

function fetchSite(siteId, params) {
    return { siteId: siteId, params: params };
}

ApiSpec.c3typ

type ApiSpec {
    name: string
}

Test API call with curl

curl -u user:pass -v -H "Content-Type: application/json"  -X GET  http://myenvironment/api/1/mytenant/mytag/topology/sites/123/buildings?name=test

You can also check this similar thread


Here is a complete example of using this annotation