.NET
Microsoft Technologies based on the .NET software framework.
3,919 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
how can I return my return await response.StatusCode;
?
Here is the code....
public async Task<IRestResponse> HitAPI(JsonData jsonSend) {
var client = new RestClient("");
var request = new RestRequest("", Method.Post):
request.AddParameter("application/json; charset=utf-8", jsonSend, ParameterType.RequestBody);
request.RequestFormat.= DataFormat.Json;
client.ExecuteAsync(request, response =>
{
if (response.StatusCode == System.Net.HttpStatusCode.Ok)
{
//it work
}
else
{
//no work
}
});
return await response.StatusCode;
}
besides all the coding errors, StatusCode is a string, and the methods return an IRestResponse
as you are using callback rather than await, chenter code hereange the code to:
public async Task<IRestResponse> HitAPI(JsonData jsonSend) {
var client = new RestClient("");
var request = new RestRequest("", Method.Post):
request.AddParameter("application/json; charset=utf-8", jsonSend, ParameterType.RequestBody);
request.RequestFormat.= DataFormat.Json;
return client.ExecuteAsync<IRestResponse>(request, response =>
{
if (response.StatusCode == System.Net.HttpStatusCode.Ok)
{
//it work
return new SomeRestResponse
{
....
}
}
else
{
//no work
return new FailedRestResponse
{
....
}
}
});
}
but using await would be cleaner:
public async Task<IRestResponse> HitAPI(JsonData jsonSend) {
var client = new RestClient("");
var request = new RestRequest("", Method.Post):
request.AddParameter("application/json; charset=utf-8", jsonSend, ParameterType.RequestBody);
request.RequestFormat.= DataFormat.Json;
var response = await client.ExecuteAsync<IRestResponse>(request);
if (response.StatusCode != System.Net.HttpStatusCode.Ok)
{
//no work
return new FailedRestResponse
{
....
}
}
//it work
return new SomeRestResponse
{
....
}
}
where SomeRestResponse and FailedRestResponse are implementations of IRestResponse