英文:
HttpClient POST response is not returning
问题
I have created an http call to an API using httpclient. Request is sending to the end point but response is not receiving. No errors
public static async Task<List<BookingFeeResponseModel>> GetBookingFeeRuleDetailsAsync(string postData)
{
try
{
HttpRequestMessage request = GetRequest(BOOKING_FEE_RULES_API_URL, postData, HttpMethod.Post);
var response = await _httpClient.SendAsync(request, CancellationToken.None);
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
var bookingFeeRules = JsonConvert.DeserializeObject<List<BookingFeeResponseModel>>(responseContent);
return bookingFeeRules;
}
}
catch (Exception ex)
{
Logger.Info($"Get Booking Fee Rule Details Err: {ex.Message}");
throw;
}
return null;
}
private static HttpRequestMessage GetRequest(string apiUrl, string postData, HttpMethod httpMethod)
{
var request = new HttpRequestMessage(httpMethod, apiUrl);
request.Headers.Accept.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Add("altHost", ALT_HOST);
if(httpMethod != HttpMethod.Get)
request.Content = new StringContent(postData, Encoding.UTF8, "application/json");
return request;
}
Here is the API which gets calling
[HttpPost]
public HttpResponseMessage GetBookingFeeAllRules([FromBody] BookingFeeRequestModel bookingFeeRequestModel)
{
try
{
Logger.Info("Getting Booking Fee All Rules");
var bookingFeeRules = new List<EditRuleModel>();
var mappedBookingFeeRules = new List<BookingFeeResponseModel>();
var allRules = GetAllRules();
var ruleIdArray = allRules.Where(r => r.ApplyOrder > 1700 && r.ApplyOrder < 1800)?.Select(a => a.Id).ToArray();
GetBookingFeeRuleDetails(bookingFeeRequestModel, bookingFeeRules, mappedBookingFeeRules, ruleIdArray, allRules);
mappedBookingFeeRules = mappedBookingFeeRules.Where(r => r.RuleModels != null)?.ToList();
var result = Request.CreateResponse(HttpStatusCode.OK, mappedBookingFeeRules);
return result;
}
catch (Exception ex)
{
Logger.Info("Error getting booking fee disclaimer {0}", ex);
throw;
}
}
Can someone please help me with this.
I need to get the API response from GetBookingFeeAllRules. But now nothing is returning. Is I am doing anything wrong here?
英文:
I have created an http call to an API using httpclient. Request is sending to the end point but response is not receiving. No errors
public static async Task<List<BookingFeeResponseModel>> GetBookingFeeRuleDetailsAsync(string postData)
{
try
{
HttpRequestMessage request = GetRequest(BOOKING_FEE_RULES_API_URL, postData, HttpMethod.Post);
var response = await _httpClient.SendAsync(request, CancellationToken.None);
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
//JObject json = JObject.Parse(responseContent);
var bookingFeeRules = JsonConvert.DeserializeObject<List<BookingFeeResponseModel>>(responseContent);
return bookingFeeRules;
}
}
catch (Exception ex)
{
Logger.Info($"Get Booking Fee Rule Details Err: {ex.Message}");
throw;
}
return null;
}
private static HttpRequestMessage GetRequest(string apiUrl, string postData, HttpMethod httpMethod)
{
var request = new HttpRequestMessage(httpMethod, apiUrl);
request.Headers.Accept.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Add("altHost", ALT_HOST);
if(httpMethod != HttpMethod.Get)
request.Content = new StringContent(postData, Encoding.UTF8, "application/json");
return request;
}
Here is the API which gets calling
[HttpPost]
public HttpResponseMessage GetBookingFeeAllRules([FromBody] BookingFeeRequestModel bookingFeeRequestModel)
{
try
{
Logger.Info("Getting Booking Fee All Rules");
var bookingFeeRules = new List<EditRuleModel>();
var mappedBookingFeeRules = new List<BookingFeeResponseModel>();
var allRules = GetAllRules();
var ruleIdArray = allRules.Where(r => r.ApplyOrder > 1700 && r.ApplyOrder < 1800)?.Select(a => a.Id).ToArray();
GetBookingFeeRuleDetails(bookingFeeRequestModel, bookingFeeRules, mappedBookingFeeRules, ruleIdArray, allRules);
mappedBookingFeeRules = mappedBookingFeeRules.Where(r => r.RuleModels != null)?.ToList();
var result = Request.CreateResponse(HttpStatusCode.OK, mappedBookingFeeRules);
return result;
}
catch (Exception ex)
{
Logger.Info("Error getting booking fee disclaimer {0}", ex);
throw;
}
}
Can someone please help me with this.
I need to get the API response from GetBookingFeeAllRules. But now nothing is returning.
Is I am doing anything wrong here?
答案1
得分: 0
你没有提供一些方法和模型,所以我根据你的逻辑构建了一个简单的示例,你可以参考它。
模型:
public class BookingFeeResponseModel
{
public int Id { get; set; }
public string Name { get; set; }
public RuleModel RuleModels { get; set; }
}
public class BookingFeeRequestModel
{
public string postData { get; set; }
}
public class EditRuleModel
{
public int Id { get; set; }
}
public class RuleModel
{
public int Id { get; set; }
public int ApplyOrder { get; set; }
}
GetBookingFeeRuleDetailsAsync方法:
[HttpPost]
public async Task<List<BookingFeeResponseModel>> GetBookingFeeRuleDetailsAsync(string postData)
{
try
{
HttpClient _httpClient = new HttpClient();
HttpRequestMessage request = GetRequest("https://localhost:7247/api/Response", postData, HttpMethod.Post);
var response = await _httpClient.SendAsync(request, CancellationToken.None);
var ResultDetail = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
var bookingFeeRules = JsonConvert.DeserializeObject<List<BookingFeeResponseModel>>(responseContent);
return bookingFeeRules;
}
}
catch (Exception ex)
{
throw;
}
return null;
}
private static HttpRequestMessage GetRequest(string apiUrl, string postData, HttpMethod httpMethod)
{
var request = new HttpRequestMessage(httpMethod, apiUrl);
request.Headers.Accept.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Add("altHost", "ALT_HOST");
BookingFeeRequestModel bookingFeeRequestModel = new BookingFeeRequestModel() { postData = postData };
if (httpMethod != HttpMethod.Get)
request.Content = new StringContent(JsonConvert.SerializeObject(bookingFeeRequestModel), Encoding.UTF8, "application/json");
return request;
}
CreateResponse扩展方法:
public static IActionResult CreateResponse(this HttpRequest request, int status, object content)
{
return new ObjectResult(content)
{
StatusCode = status
};
}
GetBookingFeeAllRules方法:
[HttpPost]
public IActionResult GetBookingFeeAllRules([FromBody] BookingFeeRequestModel bookingFeeRequestModel)
{
try
{
var bookingFeeRules = new List<EditRuleModel>();
var mappedBookingFeeRules = new List<BookingFeeResponseModel>()
{
new BookingFeeResponseModel(){ Id=1,Name = "Name1",RuleModels = new RuleModel(){ Id = 1,ApplyOrder = 1701} },
new BookingFeeResponseModel(){ Id=2,Name = "Name2",RuleModels = new RuleModel(){ Id = 2,ApplyOrder = 1801} },
new BookingFeeResponseModel(){ Id=3,Name = "Name3",RuleModels = new RuleModel(){ Id = 3,ApplyOrder = 1702} },
new BookingFeeResponseModel(){ Id=4,Name = "Name4",RuleModels = new RuleModel(){ Id = 4,ApplyOrder = 1802} },
};
var allRules = GetAllRules();
var ruleIdArray = allRules.Where(r => r.ApplyOrder > 1700 && r.ApplyOrder < 1800)?.Select(a => a.Id).ToArray();
mappedBookingFeeRules = mappedBookingFeeRules.Where(r => r.RuleModels != null)?.ToList();
var result = Request.CreateResponse(200, mappedBookingFeeRules);
return result;
}
catch (Exception ex)
{
throw;
}
}
private List<RuleModel> GetAllRules()
{
List<RuleModel> ruleModel = new List<RuleModel>()
{
new RuleModel(){ Id = 1,ApplyOrder = 1701},
new RuleModel(){ Id = 2,ApplyOrder = 1801},
new RuleModel(){ Id = 3,ApplyOrder = 1702},
new RuleModel(){ Id = 4,ApplyOrder = 1802},
};
return ruleModel;
}
希望这能帮助你。
英文:
You have not provided some methods and models, so I built a simple example based on your logic, you can refer to it.
Model:
public class BookingFeeResponseModel
{
public int Id { get; set; }
public string Name { get; set; }
public RuleModel RuleModels { get; set; }
}
public class BookingFeeRequestModel
{
public string postData { get;set; }
}
public class EditRuleModel
{
public int Id { get; set; }
}
public class RuleModel
{
public int Id { get; set; }
public int ApplyOrder { get; set; }
}
GetBookingFeeRuleDetailsAsync:
[HttpPost]
public async Task<List<BookingFeeResponseModel>> GetBookingFeeRuleDetailsAsync(string postData)
{
try
{
HttpClient _httpClient = new HttpClient();
HttpRequestMessage request = GetRequest("https://localhost:7247/api/Response", postData, HttpMethod.Post);
var response = await _httpClient.SendAsync(request, CancellationToken.None);
var ResultDetail = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
//JObject json = JObject.Parse(responseContent);
var bookingFeeRules = JsonConvert.DeserializeObject<List<BookingFeeResponseModel>>(responseContent);
return bookingFeeRules;
}
}
catch (Exception ex)
{
throw;
}
return null;
}
private static HttpRequestMessage GetRequest(string apiUrl, string postData, HttpMethod httpMethod)
{
var request = new HttpRequestMessage(httpMethod, apiUrl);
request.Headers.Accept.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Add("altHost", "ALT_HOST");
BookingFeeRequestModel bookingFeeRequestModel = new BookingFeeRequestModel() { postData = postData };
if (httpMethod != HttpMethod.Get)
request.Content = new StringContent(JsonConvert.SerializeObject(bookingFeeRequestModel), Encoding.UTF8, "application/json");
return request;
}
In the GetRequest
method, I use JsonConvert.SerializeObject(bookingFeeRequestModel)
to replace postData
, otherwise I will receive Bad Request
and cannot enter GetBookingFeeAllRules
. You can add breakpoints to see if you successfully get inside GetBookingFeeAllRules
.
Also, you can add var ResultDetail = response.Content.ReadAsStringAsync().Result;
before if (response.IsSuccessStatusCode)
to see if there is an error message.
CreateResponse extension method:
public static IActionResult CreateResponse(this HttpRequest request, int status, object content)
{
return new ObjectResult(content)
{
StatusCode = status
};
}
GetBookingFeeAllRules:
[HttpPost]
public IActionResult GetBookingFeeAllRules([FromBody] BookingFeeRequestModel bookingFeeRequestModel)
{
try
{
var bookingFeeRules = new List<EditRuleModel>();
var mappedBookingFeeRules = new List<BookingFeeResponseModel>()
{
new BookingFeeResponseModel(){ Id=1,Name = "Name1",RuleModels = new RuleModel(){ Id = 1,ApplyOrder = 1701} },
new BookingFeeResponseModel(){ Id=2,Name = "Name2",RuleModels = new RuleModel(){ Id = 2,ApplyOrder = 1801} },
new BookingFeeResponseModel(){ Id=3,Name = "Name3",RuleModels = new RuleModel(){ Id = 3,ApplyOrder = 1702} },
new BookingFeeResponseModel(){ Id=4,Name = "Name4",RuleModels = new RuleModel(){ Id = 4,ApplyOrder = 1802} },
};
var allRules = GetAllRules();
var ruleIdArray = allRules.Where(r => r.ApplyOrder > 1700 && r.ApplyOrder < 1800)?.Select(a => a.Id).ToArray();
//GetBookingFeeRuleDetails(bookingFeeRequestModel, bookingFeeRules, mappedBookingFeeRules, ruleIdArray, allRules);
mappedBookingFeeRules = mappedBookingFeeRules.Where(r => r.RuleModels != null)?.ToList();
var result = Request.CreateResponse(200, mappedBookingFeeRules);
return result;
}
catch (Exception ex)
{
throw;
}
}
private List<RuleModel> GetAllRules()
{
List<RuleModel> ruleModel = new List<RuleModel>()
{
new RuleModel(){ Id = 1,ApplyOrder = 1701},
new RuleModel(){ Id = 2,ApplyOrder = 1801},
new RuleModel(){ Id = 3,ApplyOrder = 1702},
new RuleModel(){ Id = 4,ApplyOrder = 1802},
};
return ruleModel;
}
You can first check whether the breakpoint of the called Api is hit, and then check whether the parameters can be received:
If there is no problem, you can debug step by step to ensure that there is no problem with each step:
If this breakpoint is not hit, you can view the corresponding error message to find out what the problem is:
If you don't find the problem, you can call your Api interface through Postman to see if the result is returned as you expected.
Hope this can help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论