ServiceNow API - Put & Get requests C#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2023 12:16 AM
Dear community,
i faced with very uncommon thing with servicenow API.
I making the tool for automatization of servicenow requests and using API via C#
so, whe you made PUT request to create request for example, the field requested_for should contain the sys_id of the user from sys_user table. Bur when you get the response, the field requested_for is not the sys_id, its an object! which contains sys_id AND the link.
and because of this, i faced with JSON data searilization issue, because the class structure must be different, in put its a string, in GET its an resourceLink object. here is the part of my code for explanation:
PUT. Class:
class Request
{
public string? requested_for { get; set; }
}
serialization of the class object:
HttpContent content = new StringContent(serializedObject, null, "application/json");
PUT request itself:
using var response = await _httpClient.PostAsync(
$"api/now/table/{tableName}" + (string.IsNullOrWhiteSpace(extraQueryString) ? "" : "?" + extraQueryString),
content,
cancellationToken
).ConfigureAwait(false)
?? throw new Exception("Null response.");
and in response variable this: public string? requested_for { get; set; } returned like this:
public ResourceLink<User>? requested_for { get; set; }
And the ResourceLink is like:
Class ResourceLink
{
public string? Link { get; set; }
public string? Value { get; set; }
}
P.S.
if i try to using ResourceLink in PUT request to create the request initially, its does not work. its works with string ID only.