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-27-2016 01:38 AM
Hi Adam,
Is this string correctly built with the tablename and sys_id correctly specified, in your deployed code?
string url = "https://<INSTANCENAME>.service-now.com/api/now/table/<TABLENAME>/" + currentTicket.Sys_ID;
Best Regards
Tony
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2016 08:22 AM
Hello Tony,
Thank you for your reply! I am able to copy the string and paste it into a browser and see an XML version of the record. That should mean it's correct, right? I've since moved on to using JSONv2 for the method. I'd really like to figure this out, though.
Thanks,
Adam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2016 02:49 AM
Hi Adam,
Copying the string and pasting it into a browser is quite a good troubleshooting tip I think! Yes the string must be correct.
Evidently when you invoke the browser link you have already authenticated.
Coming back to the error you saw (and still see?)
505 HTTP Version Not Supported error
There is a KB here that mentions the same error:
What version of Eureka do you have?
The workarounds, which are maybe worth trying out, mentioned are as follows:
..
Do one of the following, in order of preference:
- Ensure Basic Authentication is preemptive
- Do not specify the Expect: 100-Continue header
- Use HTTP 1.0
..
Pre-emptive Basic Auth should be deployed in any case...
Best Regards
Tony
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2016 12:57 PM
Hello Tony,
I'm on Patch 13 Hotfix2. While trying to find a solution, I've found myself back at the "400 Bad Request" error. I've attempted those workarounds, but I wasn't able to get them to work. Here is some information about the request itself: (I changed the <INSTANCENAME> and <TABLENAME> before posting here. It is correct in my code.)
RequestMessage:
{Method: PUT, RequestUri: 'https://<INSTANCENAME>.service-now.com/api/now/table/<TABLENAME>/7fe41156372fd200a457085a43990e0a',
Version: 1.1,
Content: System.Net.Http.StringContent,
Headers:
{
Accept: application/json
Content-Type: application/json; charset=utf-8
Content-Length: 408
}}
StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Transfer-Encoding: chunked
Connection: close
Date: Sat, 28 May 2016 19:39:03 GMT
Set-Cookie: JSESSIONID=EA883A8BCDG6ACE91E31347F314C4D40;Secure; Path=/; HttpOnly
Set-Cookie: BIGipServerpool_<INSTANCENAME>=998326538.34110.0000; path=/
Server: ServiceNow
Strict-Transport-Security: max-age=15768000; includeSubDomains;
Content-Type: application/json
}}
Response Headers:
{Set-Cookie: glide_user="";secure; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly, glide_user_session="";secure; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly, glide_user_route=glide.d1d022d9a3bc5bc57722bedcc93b5f46;secure; Expires=Thu, 15-Jun-2084 23:00:38 GMT; Path=/; HttpOnly
Transfer-Encoding: chunked
Date: Sat, 28 May 2016 19:46:33 GMT
X-Cnection: close
Server: ServiceNow
Strict-Transport-Security: max-age=15768000; includeSubDomains;
}