Próbuję zrobić bardzo prosty przykład użycia metody Exspute RestSharp do sprawdzania reszty punktów końcowych i serializacji do POCO. Jednak wszystko, co wypróbuję, powoduje pojawienie się obiektu response.Data, który ma wszystkie właściwości z wartością NULL.Klient RestSharp zwraca wszystkie właściwości jako wartość NULL przy deserializacji odpowiedzi JSON
Oto odpowiedź JSON:
{
"Result":
{
"Location":
{
"BusinessUnit": "BTA",
"BusinessUnitName": "CASINO",
"LocationId": "4070",
"LocationCode": "ZBTA",
"LocationName": "Name of Casino"
}
}
}
Oto mój kod testowy
[TestMethod]
public void TestLocationsGetById()
{
//given
var request = new RestRequest();
request.Resource = serviceEndpoint + "/{singleItemTestId}";
request.Method = Method.GET;
request.AddHeader("accept", Configuration.JSONContentType);
request.RootElement = "Location";
request.AddParameter("singleItemTestId", singleItemTestId, ParameterType.UrlSegment);
request.RequestFormat = DataFormat.Json;
//when
Location location = api.Execute<Location>(request);
//then
Assert.IsNotNull(location.LocationId); //fails - all properties are returned null
}
I tu jest mój kod API
public T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient();
client.BaseUrl = Configuration.ESBRestBaseURL;
//request.OnBeforeDeserialization = resp => { resp.ContentLength = 761; };
var response = client.Execute<T>(request);
return response.Data;
}
I wreszcie, tutaj jest moje POCO
public class Location
{
public string BusinessUnit { get; set; }
public string BusinessUnitName { get; set; }
public string LocationId { get; set; }
public string LocationCode { get; set; }
public string LocationName { get; set; }
}
Ponadto właściwości ErrorException i ErrorResponse dla odpowiedzi mają wartość NULL.
Wydaje się to bardzo prostym przypadkiem, ale biegam w kółko przez cały dzień! Dzięki.
Co się dzieje, gdy wywołujesz 'request.AddUrlSegment (" singleItemTestId ", singleItemTestId)' zamiast wywołania funkcji 'AddParameter'? –