Methods

(async) onRequest(context, url, request) → {Promise.<Request>}

Design for "Rewrite Script". This function is called before the HTTP request is sent to the server. If you do not modify the request, you should delete this function to avoid unnecessary calls.
Parameters:
NameTypeDescription
contextRewriteContextThe context object.
urlstringThe URL of the HTTP request.
requestRequestThe HTTP request object.
Returns:
- A promise that resolves when the function execution is completed.
Type: 
Promise.<Request>
Examples
// modify request body
async function onRequest(context, url, request) {
    // parse to Text
    var text = request.body.parseText()
    console.log(text)
    request.body = "this is a replaced request text"

    // parse to JSON
    var json = request.body.parseJSON()
    json["script_json"] = "JSONValue"
    request.body = json

    // parse to URLSearchParams
    var urlencoded = request.body.parseURLSearchParams()
    urlencoded.append("script", "parseURLSearchParams")
    request.body = urlencoded
    // Done
    return request;
}
// redirect、modify path、modify queries、modify headers
async function onRequest(context, url, request) {
    console.log(url);
    // set parameter, you can get it in onResponse
    context.parameters["custom"] = "custom parameter on request"
    
    // redirect
    request.scheme = "http" // change to https
    request.path = "/upload/json" // path change to /upload/json
    request.host = "192.168.3.30" // host change to 192.168.3.30
    request.port = 6000 // port change to 6000
    // change method
    request.method = "POST" // change method to POST
    
    // queries operation
    // add query
    request.queries.append("name", "value1")
    // replace or add query
    request.queries.set("name", "value2")
    // get query with name
    request.queries.get("name")
    // delete query with name
    request.queries.delete("name")
    
    // headers operation
    // delete headers named accept
    request.headers.delete("accept")
    // add name value to headers
    request.headers.append("hodor", "custom value")
    // print all headers
    var headers = ""
    request.headers.forEach((value, name) => {
        headers = headers + name + ":" + value
    });
    console.log(headers);
    // Done
    return request;
}

(async) onResponse(context, url, response) → {Promise.<Response>}

Design for "Rewrite Script". This function is called before the HTTP response is sent to the client. If you do not modify the response, you should delete this function to avoid unnecessary calls.
Parameters:
NameTypeDescription
contextRewriteContextThe context object.
urlstringThe URL of the HTTP response.
responseResponseThe HTTP response object.
Returns:
- A promise that resolves when the function execution is completed.
Type: 
Promise.<Response>
Examples
// response body
async function onResponse(context, url, response) {
    // parse to Text
    var text = response.body.parseText()
    console.log(text)
    response.body = "this is a replaced response text"

    // parse to JSON
    var json = response.body.parseJSON()
    json["script_json"] = "JSONValue"
    response.body = json

    // parse to URLSearchParams
    var urlencoded = response.body.parseURLSearchParams()
    urlencoded.append("script", "parseURLSearchParams")
    response.body = urlencoded
    // Done
    return response;
}
// code、headers
async function onResponse(context, url, response) {
    // get parameters setting in onRequest
    console.log(context.parameters["custom"]) // Output "custom parameter on request"
    // match url
    console.log(url);
    // redirect
    response.statusCode = 200 // change status to 200
    response.statusPrase = "OK" // change statusText to OK
    // headers operation
    // delete headers named accept
    response.headers.delete("accept")
    // add name value to headers
    response.headers.append("hodor", "custom value")
    // print all headers
    var headers = ""
    response.headers.forEach((value, name) => {
        headers = headers + name + ":" + value
    });
    console.log(headers);
    // Done
    return response
}

(async) onSchedule(context) → {Promise.<void>}

Design for "Schedule Script". This function will be called at the scheduled timer trigger time. It is mainly used to perform some scheduled tasks.
Parameters:
NameTypeDescription
contextScheduleContextThe context object.
Returns:
- A promise that resolves when the function execution is completed.
Type: 
Promise.<void>
Example
async function onSchedule(context) {
    /// set custom parameters for next runloop
    context.parameters["custom"] = "custom parameters for next runloop";
    /// repeatedCount
    console.log(`repeatedCount ${context.repeatedCount}`);
    /// if repeatedCount is 2, abort schedule
    if (context.repeatedCount == 2) {
        abort()
    }
    // you also an use HTTPClient to send request
    const rsp = await HTTPClient.get("https://www.apple.com")
    let text = rsp.body.parseText()
    console.log(text)
}

(async) onSessionCompleted(context, url, request, response) → {Promise.<void>}

Design for "HTTP Completed Script". This function is called after the HTTP response is returned. It can be used for HTTP data collection.
Parameters:
NameTypeDescription
contextCompletedContextThe context object.
urlstringThe URL of the HTTP request.
requestRequestThe HTTP request object.
responseResponseThe HTTP response object.
Returns:
- A promise that resolves when the function execution is completed.
Type: 
Promise.<void>
Example
// sample of pass data to local http server
async function onSessionCompleted(context, url, request, response) {
    try {
        // parse request body to text
        var reqText = request.body.parseText();
        // parse response body to text
        var rspText = response.body.parseText()
        var params = {
            "body" : { // HTTPClient post body data
                "url": url, // request url
                "reqHeader": request.headers.encode(), // request headers
                "reqText": reqText, // request body
                "rspHeader": response.headers.encode(), // response headers
                "rspText": rspText, // response body
            },
            "headers": { // HTTPClient headers
                "from": "onSessionCompleted"
            },
        }
        // send http session data to other server
        const rsp = await HTTPClient.post("http://192.168.3.30:8000/upload", params)
        // parse response body to text and print
        let text = rsp.body.parseText()
        console.log(text)
    } catch(error) {
        console.log(error)
    }
}