Calling Table API from C#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2016 12:23 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2016 09:56 PM
Hi Adam,
I carried out a put towards a geneva instance, building the query with REST API Explorer.
If you think that is a useful thing to do you could get a personal developer instance.
Here is the equivalent curl and business rule code.
curl "https://<name>.service-now.com/api/now/table/incident/d71f7935c0a8016700802b64c67c11c6" \
--request PUT \
--header "Accept:application/json"\
--header "Content-Type:application/json" \
--data "{\"short_description\":\"TEST1\"}" \
--user 'admin':'admin'
Not sure what is wrong with your put - looks like it is http v 1.1 - http v 1.0 was mentioned a workaround - but you now see a different error in any case.
the equivalent curl command make no reference to charset=utf-8
Maybe try a get or post rather that a put as a diagnostic step?
Best Regards
Tony
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2016 12:54 PM
Tony,
I am able to use Chrome's Advanced Rest Client to perform PUTs and GETs. I have tried every combination of solutions I can find on Stack Overflow and Wiki. Here's my latest snippet. This is now giving me a 401 "Unauthorized" error. The account has the rest_service and web_service_admin roles. I'm at a complete loss here. I've tried using HttpWebRequest and XMLHTTP as well with no success. So, I came back to HttpClient.
using (HttpClient rest = new HttpClient())
{
Uri sNowURI = new Uri(url);
rest.BaseAddress = sNowURI;
rest.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", Environment.GetEnvironmentVariable("UserName"), <PASSWORD>))));
rest.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await rest.GetAsync(sNowURI);
response.EnsureSuccessStatusCode();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2016 03:46 PM
I'm also having this particular error when trying to just use a button to find out if it will work or not in the code. Here is what I have so far:
protected void Button1_Click(object sender, EventArgs e)
{
string URI = "https://<INSTANCE>.service-now.com/api/now/v2/table/<table_name>?sysparm_limit=1";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.Accept] = "application/json";
// wc.Headers[HttpRequestHeader.ContentType] = "application/json";
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("<input username here>:<input password here>"));
wc.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", credentials);
string HtmlResult = wc.UploadString(URI, "+ u_vndr_id");
}
}
}
My problem occurs in the very last line of code. Imagine a blank Web Form.aspx web page being loaded with a button on the top left hand corner, that's all it is. So when I press the button it runs through this portion of code. It tells me that I have a 400 bad request as well.
What would be the proper way to write the request portion in C# for SN? The code snippets help you for the scripting languages but when it comes to integration with other languages there no examples.
Any help would be really appreciated!
Thanks,
Orlando
EDIT: For the string HtmlResult, I have tried various things with the second parameter. +u_vndr_id is just something I left inside. I have also tried this for example:
?sysparm_fields=vendor&sysparm_limit=1 , in the second parameter whilst removing the question mark from the URI and trying to get this portion to work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2016 03:14 AM
Actually I Want to call Table API to retrieve records from c# - Reply will be highly appreciated?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2016 12:58 PM
Hello Harpreet,
Are you able to get the code I originally posted to work? I am able to query a table and make changes with JSONv2 and XMLHTTP. I'm just hoping to use the table API in this case. I can provide you a snippet of what I use for JSONv2 if you need me to.
Adam