I have a request object below:
public class SearchRequest
{
[JsonProperty(PropertyName = "search_value")]
public string Value { get; set; }
}
and a web api endpoint that expects
[HttpPost]
[Route("~/api/city/search")]
public SearchResponse Search(SearchRequest request)
{
try
{
var result = _cityService.Search(request);
return result;
}
catch (Exception ex)
{
throw ex;
}
}
which I call using the following
var serialisedRequest = JsonConvert.SerializeObject(request);
which serialises the request like {"search_value":"test"}.
var content = new StringContent(serialisedRequest, Encoding.UTF8, "application/json");
using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }))
{
SetupHttpClient(client);
var fullUrl = string.Format("{0}/{1}", baseUri, svcEndPoint);
var request = new HttpRequestMessage { Content = content, Method = HttpMethod.Post, RequestUri = new System.Uri(fullUrl) };
using (var result = await client.SendAsync(request))
{
if (result.IsSuccessStatusCode)
{
var content = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<TResponse>(content);
}
}
}
When I call the API and put a break point in the API controller, the value for Value is null.
Any reason why this would be?
public SearchResponse Search([FromBody]SearchRequest request)?[JsonPropertyName("search_value")] public string Value { get; set; }JsonPropertyNameis the answer here