This is my Api Controlller Method which is talking to a Rest api Url.I am sending all the necessary data to do a search on this url.Now, Everything is Ok and it returns back Http Status Code 200 with proper Response.The Sample code is:
[System.Web.Http.HttpPost]
public async Task<HttpResponseMessage> SearchFlight([FromBody]SearchForFlight sof)
{
string url = "http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/Search/";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new
System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var payload = JsonConvert.SerializeObject(sof);
var response = await client.PostAsync(url, new StringContent(payload, Encoding.UTF8, "application/json"));
var json = await response.Content.ReadAsStringAsync();
HttpResponseMessage res = Request.CreateResponse(HttpStatusCode.OK, json);
return res;
}
}
I am serializing the sof object and posting back to client object:
var response = await client.PostAsync(url, new StringContent(payload, Encoding.UTF8, "application/json"));
Now, If i try to read the content in string form from this response using this:
var json = await response.Content.ReadAsStringAsync();
Now, here i am not getting proper Response. The Response i get back is:
"{\"Response\":{\"ResponseStatus\":3,
\"Error\":{\"ErrorCode\":3,\"ErrorMessage\":\"Please specify Flight Segment.\"},
\"TraceId\":\"98b33a39-5dd9-48fa-999c-0eb5c99adef1\"}}"
The Problem is here. According to documentation, Segment object should be something like this:
"Segments": [
{
"Origin": "DEL",
"Destination": "BOM",
"FlightCabinClass": "1",
"PreferredDepartureTime": "2015-11-06T00: 00: 00",
"PreferredArrivalTime": "2015-11-06T00: 00: 00"
}
],
While i am sending back data to web api controller using this code.Here i have omitted []array symbol from Segments object.
<script>
$(document).ready(function () {
$("#btnPost").click(function () {
var sof = {
AdultCount: $("#AdultCount").val(),
JourneyType: $("#JourneyType :selected").text(),
PreferredAirlines: null,
Segments: {
Origin: $("#Origin").val(),
Destination: $("#Destination").val(),
FlightCabinClass: $("#FlightCabinClass").val(),
PreferredDepartureTime: $("#PreferredDepartureTime").val(),
PreferredArrivalTime: $("#PreferredArrivalTime").val(),
},
};
If i did not omit this []array symbol, then i cant post back proper data to web api controller.If I omit then i am getting error in response. how to assign values to nested object through json object array I was told to omit as u can see in this question. Please some one help me.