Home > front end >  POST Rest API call to servicenow using C#
POST Rest API call to servicenow using C#

Time:10-03

I am trying to send information to the event queue in servicenow as below. But I get at message as

Id = 3, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}".

Any suggestions?

public void PostEvent()     
{            
    string jsonFilePath = @"{""records"": [{""source"": ""Test"",""event_class"": ""Test Class"",""node"": ""test"",""message_key"": ""Test Incident for Test 1"",""severity"": ""5"",""description"": ""Test Incident for Test 1 Desc"",""additional_info"":'{""name"": ""Test System"",""short_des"": ""Test Incident for Test 1 Short Desc"",""ASSIGNMENT_GROUP"": ""xyz""}'}]}";
            
    var client = new HttpClient();
    client.BaseAddress = new Uri("https://abcdev.service-now.com/api/global/em/jsonv2");
    client.DefaultRequestHeaders.Clear();
    var jsonBody = (new JavaScriptSerializer()).Serialize(jsonFilePath);
    client.PostAsync("test", new StringContent(jsonBody, Encoding.UTF8, "application/json"));
}

CodePudding user response:

You are using the async version of Post without awaiting it. Make your method async Task and await/ or put .Result after your last line.

CodePudding user response:

You are using async version of POST, So you have to create function as async and you have to await for Post function.

public async Task PostEvent()     
{            
    string jsonFilePath = @"{""records"": [{""source"": ""Test"",""event_class"": ""Test Class"",""node"": ""test"",""message_key"": ""Test Incident for Test 1"",""severity"": ""5"",""description"": ""Test Incident for Test 1 Desc"",""additional_info"":'{""name"": ""Test System"",""short_des"": ""Test Incident for Test 1 Short Desc"",""ASSIGNMENT_GROUP"": ""xyz""}'}]}";
            
    var client = new HttpClient();
    client.BaseAddress = new Uri("https://abcdev.service-now.com/api/global/em/jsonv2");
    client.DefaultRequestHeaders.Clear();
    var jsonBody = (new JavaScriptSerializer()).Serialize(jsonFilePath);
    await client.PostAsync("test", new StringContent(jsonBody, Encoding.UTF8, "application/json"));
}

You can also use async version of post without await keyword. but for that you have to add aviator Result. this will wait until request execution will get completed.

public void PostEvent()     
{            
    string jsonFilePath = @"{""records"": [{""source"": ""Test"",""event_class"": ""Test Class"",""node"": ""test"",""message_key"": ""Test Incident for Test 1"",""severity"": ""5"",""description"": ""Test Incident for Test 1 Desc"",""additional_info"":'{""name"": ""Test System"",""short_des"": ""Test Incident for Test 1 Short Desc"",""ASSIGNMENT_GROUP"": ""xyz""}'}]}";

    var client = new HttpClient();
    client.BaseAddress = new Uri("https://abcdev.service-now.com/api/global/em/jsonv2");
    client.DefaultRequestHeaders.Clear();
    var jsonBody = (new JavaScriptSerializer()).Serialize(jsonFilePath);
    client.PostAsync("test", new StringContent(jsonBody, Encoding.UTF8, "application/json")).Result;
}
  •  Tags:  
  • c#
  • Related