Calling Table API from C#

adam_faulkinber
Kilo Expert

I'm trying to use a put request via C# HttpClient.   I keep getting 400 Bad Request in my response.   I've done everything I can find on the wiki with no success.   Can anyone tell me what I'm missing?   This is for a Eureka instance.   Here is my code:

                    string postText = "{\"assigned_to\":\"" + MySydId + "\"," +

                                                                "\"state\":\"3\"," +

                                                                "\"cmdb_ci\":\"" + configItem + "\"," +

                                                                "\"comments\":\"" + comments + "\"}";

                      string url = "https://<INSTANCENAME>.service-now.com/api/now/table/<TABLENAME>/" + currentTicket.Sys_ID;

                      HttpContent content = new StringContent(JsonConvert.SerializeObject(postText), Encoding.UTF8, "application/json");

                      HttpClientHandler restHandler = new HttpClientHandler { Credentials = new NetworkCredential(Environment.GetEnvironmentVariable("UserName"), <PASSWORD>) };

                      using (HttpClient rest = new HttpClient(restHandler))

                      {

                              Uri sNowURI = new Uri(url);

                              rest.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                              HttpResponseMessage response = rest.PutAsync(sNowURI, content).Result;

                              response.EnsureSuccessStatusCode();

                      }

The table is a custom table.       The account I am using has the rest_service role and I am able to see the xml data for the record when I paste the url into my browser.

After posting this, I saw that I was missing a comma in the JSON string.   Now I am getting a 505 HTTP Version Not Supported error.  

10 REPLIES 10

Thanks a lot Adam, Yes I have used table API and add Configuration settings for Cross Origin Resource Sharing(CORS) in ServiceNow. Then it works fine for me.