簡體   English   中英

POST Rest API 調用 servicenow 使用 C#

[英]POST Rest API call to servicenow using C#

我正在嘗試將信息發送到 servicenow 中的事件隊列,如下所示。 但我收到的消息是

Id = 3,狀態 = WaitingForActivation,方法 =“{null}”,結果 =“{尚未計算}”。

有什么建議么?

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"));
}

您正在使用 Post 的異步版本而不等待它。 使您的方法async Taskawait /或在最后一行之后放置.Result (這樣您就可以等待同步代碼中的異步調用)。 但是更喜歡 await 而不是 Result(錯誤處理不同)

您正在使用POST的異步版本,因此您必須將 function 創建為異步版本,並且必須等待 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"));
}

您還可以使用不帶 await 關鍵字的 post 的異步版本。 但為此你必須添加 aviator Result 這將等到請求執行完成。

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM