Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

ServiceNow API - Put & Get requests C#

METAH
Giga Contributor

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.

 

 

 

0 REPLIES 0