Request
$request is a function that sends an HTTP request with specified parameters and returns the JSON response. It's a utility function to make HTTP requests to APIs.
Parameters
URL: The URL to which the HTTP request is to be made.
Method: The HTTP method to use for the request. The options are "GET", "POST", "PUT", "DELETE".
Data: (optional) The data to send with the request. This is typically used with "POST" and "PUT" requests.
Headers: (optional) Any headers that should be sent with the request. This should be a stringified JSON object.
Return Value
This function returns the server's response as a JSON object.
Example
log($request("https://jsonplaceholder.typicode.com/posts", "POST", '{"title":"test", "body":"content", "userId":1}', '{"Content-type": "application/json; charset=UTF-8"}'))
The script above sends a "POST" request to the specified URL with the given data and headers. The server's response is then logged.
Please note that the function may fail if the server doesn't exist, the server doesn't respond, or if the response is not JSON.
It might be wise to catch any errors that occur when using the $request function and handle them appropriately in your script.
For example, the function will throw an error if the response cannot be parsed as JSON, so you can use a 'try-catch' block to handle this error in your script.
Remember: When using an API, ensure you are authorized to use it, understand the cost that may be associated with its use and are obeying all its usage policies and guidelines.