Unable to parse JSON for a field in REST message using setRequestBody method

GD11
Tera Expert

We are unable set parameters in request Body.

error message:

GD11_0-1752158380774.png

logs are printing fine.

 
 

value of body input_data={"request":{"subject":"Product Notice: The status of MOTORCYCLEACCIDENT.COM has changed.","description":"Product Notice: The status of MOTORCYCLEACCIDENT.COM has changed.\n\nYou can see this change in your account or get more details inside. ‌ ‌ ‌ ‌ ‌ ‌ \n\n ‌ ‌ ‌ ‌ ‌ ‌ ‌

 

rest message function.

 createIncident: function(current) {

        var subject = current.short_description.toString();
        var snTicketSysId = current.sys_id.toString();
        var catValue = current.category.getDisplayValue();
        var subCatValue = current.subcategory.getDisplayValue();
        var caller = current.caller_id.getDisplayValue();
        var affectedUser = current.u_affected_user.getDisplayValue();
        var description = current.description.toString();
        var snTicketNumber = current.number.toString();
        var assignmentGroup = current.assignment_group.getDisplayValue();

        // var currentPriority = [];
        // currentPriority.push(current.priority.getDisplayValue());
        // gs.log('priority value ' + currentPriority.toString());
        // currentPriority = currentPriority.toString().trim();
        // var priorityValue = currentPriority.split('-');
        // priorityValue = priorityValue[1];
        // gs.log('priority value 1 ' + priorityValue[1]);

        var requestObj = {};
        requestObj = { // code to generate payload for all variables required.
            "request": {
                "subject": subject,
                "description": description,
                "requester": {
                    "email_id": "Amynta.SDP@fulcrumdigital.com"
                },
                "status": {
                    "name": "New"
                },
                "udf_fields": {
                    "udf_char32": snTicketSysId,
                    "udf_char44": assignmentGroup,
                    "udf_char42": caller,
                    "udf_char22": affectedUser,
                    "udf_char21": snTicketNumber
                },
                "template": {
                    "name": "***_Cloud_Incidents"
                },
            }
        }
        gs.log('function being called ');
        var inputData = "input_data="+ JSON.stringify(requestObj);
        //inputData = JSON.stringify(inputData);
        gs.log('value of body ' + inputData);

        try {
            var r = new sn_ws.RESTMessageV2('***Service Desk Integration', 'Create Request and Incident');
            r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            r.setRequestHeader('accept', 'application/vnd.manageengine.sdp.v3+json');
            r.setRequestHeader("Authorization", "Bearer " + gs.getProperty('Manage.Engine.SDP.AccessToken'));
            r.setAuthentication("oauth2", "Manage Engine Service Desk Integration default_profile");
            //r.setStringParameterNoEscape('input_data', JSON.stringify(requestObj));
            r.setRequestBody(inputData);           

            var response = r.execute();
            var responseBody = response.getBody();
            gs.log('value of response body ' + responseBody);
            var incidentResBody = JSON.parse(responseBody);
             var httpStatus = response.getStatusCode();
            gs.log('value of status ' + httpStatus);
        } catch (ex) {
            var message = ex.message;
            gs.log('value of error message ' + message);
        }
 
 
 
 
 

 

 

 

 

7 REPLIES 7

Vishal Jaswal
Giga Sage

Hello @GD11 

The value of body input_data is in JSON format, hence you should update this:

r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

to this:

r.setRequestHeader("Content-Type", "application/json");

and then replace this:

var inputData = "input_data="+ JSON.stringify(requestObj);

to this:

var inputData = JSON.stringify(requestObj);




Hope that helps!

Ankur Bawiskar
Tera Patron
Tera Patron

@GD11 

can you share what you have configured in REST Message method in Content body?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar  and @Vishal Jaswal  i tried suggestion of vishal and it is giving different error now.

error: value of response body {"response_status":{"status_code":4000,"messages":[{"status_code":4001,"field":"input_data","type":"failed","message":"Value not present for input_data"}],"status":"failed"}}

 

sharing rest message method configuration.

GD11_0-1752164211489.png

 

Hi @Ankur Bawiskar  and @Vishal Jaswal  i tried suggestion of vishal and it is giving different error now.

error: value of response body {"response_status":{"status_code":4000,"messages":[{"status_code":4001,"field":"input_data","type":"failed","message":"Value not present for input_data"}],"status":"failed"}}

 

sharing rest message method configuration.

GD11_0-1752164211489.png