POST Method
The post
method is used to send HTTP requests of type POST
. It takes a URL
, data, and optional request parameters, returning the result of the server's request processing.
Function Overloads for post
declare function reqPost(url: string, data?: BodyData, options?: NoMethod<TextResponseOptions>): Promise<Response<string>>;
-
url:
string
- a string representing the URL of the resource. -
data:
BodyData
- the data to be sent. -
options:
NoMethod<TextResponseOptions>
- optional request parameters.
Returns an object of type Response<string>
.
Properties
url: string
The URL of the resource being requested.
Example values:
'https://data.chatium.com/samples/menu.json'
data: BodyData
The data sent in the body of the request. The data type can be one of the following:
string
Record<string, unknown>
unknown[]
null
Example values:
'{ "name": "John", "age": 30 }'
[1, "string", true, { key: "value" }]
options
An optional object containing request parameters.
Properties of the options object
-
headers:
Record<string, string | string[] | undefined>
HTTP request headers.
Example value:
{ 'Content-Type': 'application/json', 'Authorization': 'Bearer token' }
-
searchParams:
string | Record<string, string | number | boolean | null | undefined>
Query string parameters.
Example value:
{ lang: 'en', page: 1 }
-
responseType:
'json' | 'text'
The type of data in the response.
-
timeout:
number
The timeout for the request in milliseconds.
Example value:
5000
Return Value
The response from the server.
Response
The response object from the server.
Properties of the Response object
-
body:
The body of the response, the type is determined when the function is called.
-
statusCode:
number
The HTTP status code.
-
headers:
Record<string, string>
The response headers.
Example
Let's consider an example of using the POST
method to add a new item to a menu. This uses a library for sending HTTP
requests.
This asynchronous function sends a POST
request to the specified URL and returns the result of the server's request processing.