The code in every language
Each example is the inside of main, written in every language API Maker runs : pick yours in the tabs, and every page shows it from then on. The types a call takes (GetAll, DeepItem…) are those of am : from am import GetAll in Python, use am::GetAll; in Rust, import am.GetAll; in Java and Kotlin ; PHP and C# have them without an import, Go names them am.GetAll.
The answer : g.res¶
Custom APIs and hooks shape the answer of the request through g.res.
Output¶
Response
Status code¶
statusCode use to set/get status code of the response.
Response
All supported status code.
OK = 200,
NO_CONTENT = 204,
CREATED = 201,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
FORBIDDEN = 403,
RESOURCE_NOT_FOUND = 404,
INTERNAL_SERVER_ERROR = 500
Errors¶
Response
{
"success": false,
"statusCode": 400,
"errors": [
{
"code": 400,
"message": "Error message here."
}
],
"warnings": []
}
Warnings¶
Response
{
"success": true,
"statusCode": 200,
"data": {
"hello": "v2"
},
"warnings": [
{
"code": 401,
"message": "This is warning!"
}
]
}
Shared between hooks and code¶
g.res.shared is used to share data between post-hooks, pre-hooks, and code within that API.
Response
Content types¶
- The user does not need to set the response content type to send JSON as a response.
- If the response content type is not set that means it is application/JSON.
If ContentType is set to below system will return data accordingly.
JSON = 'application/json'
XML = 'text/xml'
YAML = 'text/yaml'
TEXT = 'text/plain'
HTML = 'text/html'
Note: If content type is set and not from above list, then API Maker will consider a text the code returns as base64 encoded. Bytes the code returns (a
Buffer,bytes,byte[],[]byte…) are sent as they are, whatever the content type : see OCTET_STREAM below.
HTML¶
Response
JSON¶
Response
{
"success": false,
"statusCode": 555,
"data": "World",
"errors": [
{
"code": 401,
"message": "This is error"
}
],
"warnings": []
}
Bytes : application/octet-stream¶
| Content type OCTET_STREAM | |
|---|---|
| Content type OCTET_STREAM | |
|---|---|
| Content type OCTET_STREAM | |
|---|---|
| Content type OCTET_STREAM | |
|---|---|
| Content type OCTET_STREAM | |
|---|---|
| Content type OCTET_STREAM | |
|---|---|
| Content type OCTET_STREAM | |
|---|---|
| Content type OCTET_STREAM | |
|---|---|
- It will download the image.
- It will send the bytes as they are : a file the code read or downloaded, a PDF it made. Return the bytes (a
Buffer,bytes,byte[],[]byte), or leave them ing.res.output: the answer is those bytes, under the content type the code names, whatever it is (image/png,application/pdf…). Headers of the code go with them (Content-Disposition,Cache-Control).
Text¶
Response
XML¶
Response
<?xml version='1.0'?>
<root>
<success>true</success>
<statusCode>200</statusCode>
<data>Hello world from the API Maker.</data>
</root>
YAML¶
Response