cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

JSON.stringify() on requestBody in pre-execution script of HTTP monitor ?

iromaniuk
Newcomer

I am using request.setBody(requestBody) , but getting Bad request response code 400. I can see [object Object] in the synthetic.0.0.log when logging RequestBody

2020-11-03 16:48:11 UTC WARNING [<collector>] [<collector.core>, LogEntryService] Http-Executor-0 [CommonApiController] [HTTP_CHECK-A12AC65899713EC, HTTP_CHECK_STEP-CC3B186211915B8C] RequestBody: [object Object]

See the code below. Should I JSON.stringify() on requestBody , what's wrong here ?

var currSessions=api.randomNextInt();
api.warn("CurrSessions: " + currSessions);
var timestampNow = api.date();
api.warn("Timestamp: " + timestampNow);
var requestBody={
    "type": "LB",
    "series": [
        {
            "timeseriesId": "custom:alteon.CurrSessions",
            "dimensions": {
                "CUSTOM_DEVICE": "Load Balancers"
            },
            "dataPoints": [
                [
                    timestampNow,
                    currSessions
                ]
            ]
        }
    ]
};
api.warn("RequestBody: " + requestBody);
request.setBody(requestBody);
2 REPLIES 2

leon_vanzyl
Advisor

Hi, @iromaniuk  did you ever figure this out? I was escaping the characters but still no joy

Ok, I resolved this by using Postman, it has handy code functionality that will escape the value of the JSON request body for you. Recreate the request in postman then select code by clicking on the code link under the send button.

Just select javascript - Fetch on the "generate code snippets" page. This will escape your raw Json nicely 😉