Problem description
An HTML5 application needs to call a Mobile Gateway Service API using a JavaScript API. The server-side API accepts POST requests. This topic describes how to construct the POST request in the HTML5 application.
Server-side configuration
API description
Suppose that the API is described as follows:
POST /api/product
{
"id": 0,
"productName": "Mask"
}This API accepts a POST request. The request must include the id and productName parameters in the POST body.
Data model configuration
First, create a data model for the POST body based on the API description.
In the mPaaS console, choose Mobile Gateway Service > Data Model > Create Data Model, and then configure the model.
Configure the gateway API
Navigate to the Background connection > Mobile Gateway Service > Manage API > Create API page and complete the following steps:
Modify the basic API information as needed.
Configure the request parameters. Set the request body type to the data model that you created in the previous step. Configure the return value and message type as needed.
HTML5 client call
When you send a request using a JavaScript API, place the POST body content in the requestData parameter as the value of _requestBody. For example:
window.AlipayJSBridge.call('rpc', {
operationType: 'com.mpaas.addProduct',
requestData: [
{"_requestBody":"{\"id\":\"1\",\"name\":\"mask\"}"}],
headers:{},
getResponse: true
}, function(data) {
alert(JSON.stringify(data));
});