API Request function is not working in Servicenow

Originally asked by Harold Oconitrillo on 09 October 2023 (original question)


Hi Team,

We have to update the data with the import set table. while trying to access the via api request http client post method its not working .

Can you please let us know the issue on this.

def postitem = httpClient.http(“POST”, “/api/now/import/temp_risks”,
“”“{“u_project”: replica.project?.key ,“u_correlation_id”: replica.key ,“u_impact”: priorityMapping[priorityName] ,“u_short_description”: replica.summary ,
“u_description”: replica.description ,“u_mitigation_plan”: replica.mitigation ,“u_due_date”: targetDate ,“u_show_on_status_report”: “true” ,
“u_risk_state”: statusMapping[remoteStatusName]”}“”",
[“sysparm_display_value”: [“true”]],
[“Content-Type”:[“application/json”]])
{
response ->
if (response.code ==200)
response.body
else throw new Exception(“Failed with ${response.body} and body: ${response.body}”)
}


Answer by Javier Pozuelo on 07 December 2023

Implement the following modifications to the script

def postitem = httpClient
  .http(
    "POST", 
    "/api/now/import/temp_risks",
    groovy.json.JsonOutput
      .toJson(
        [
          "u_project": replica.project?.key,
          "u_correlation_id": replica.key,
          "u_impact": priorityMapping[priorityName],
          "u_short_description": replica.summary,
          "u_description": replica.description,
          "u_mitigation_plan": replica.mitigation,
          "u_due_date": targetDate,
          "u_show_on_status_report": "true",
          "u_risk_state": statusMapping[remoteStatusName]
        ]
      ),
    ["sysparm_display_value": ["true"]],
    ["Content-Type":["application/json"]]) { request, response ->
  if (response.code ==200) response.body
  else throw new Exception("Failed with ${response.body} and body: ${response.body}")
}