API Call can be simplified?
Hi,
I was wondering if this API Call could be simplified and if i really necessary to use JArray?
I had a hard time getting it to work, and now every API-Call isnt looking that clean.
Would it be safe to add a service for the calls? I got this Controller [authorized], is that something i need for a service?
I need to add Accept application/json and Content-Type application/json to the header in postman, otherwise it returns xml.
using (HttpResponseMessage response = await _httpClient.PostAsync($"{ipAdress.IpAddress}/safe/Person/accesspointstate/fetch?api_key={_apiKey.APIKey}", jsonContent))
{
if (response.IsSuccessStatusCode)
{
string jsonResponse = ReplaceSpecialChars(await response.Content.ReadAsStringAsync());
JArray jsonArray = JArray.Parse(jsonResponse);
foreach (JObject jsonObject in jsonArray)
{
if (jsonObject.ToString().Contains("Error"))
{
continue;
}
else
{
DoorModel door = new DoorModel
{
Id = jsonObject["Id"].ToString(),
DoorName = jsonObject["DoorName"].ToString(),
Blocked = jsonObject["Blocked"].ToString(),
Ilac = jsonObject["Ilac"].ToString(),
IpAddress = ipAdress.IpAddress.ToString(),
};
_doorList.Add(door);
}
}
}
1 Reply
If the API is sane, you can just deserialize this Json to a class instead of the
JArray
tomfoolery