-
Mar 23rd, 2020, 03:21 AM
#1
Thread Starter
Fanatic Member
How to call another Web API within Web API?
Hi All,
How to call another Web API (the same box or other box) within Web API?, codes below does not work
[HttpPost]
[Route("/B2B/Core_RegistrationAsync")]
[AllowAnonymous]
public async Task<object> Core_RegistrationAsync([FromBody]JObject _jo)
{
try
{
string _company_name = (string)_jo["company_name"];
string _company_email = (string)_jo["company_email"];
string _core_client_id = (string)_jo["core_client_id"];
using (var client = new HttpClient())
{
var url = new Uri(_url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("api_key", Models.AppSettings.Application.PTDigital.B2B_API_KEY);
client.DefaultRequestHeaders.Add("api_secret", Models.AppSettings.Application.PTDigital.B2B_API_SECRET);
var obj = new
{
client_id = _core_client_id,
company_name = _company_name,
company_email = _company_email
};
var json = JsonConvert.SerializeObject(obj);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(_url, content);
HttpStatusCode _statusCode = response.StatusCode;
if (_statusCode == HttpStatusCode.OK)
{
response.EnsureSuccessStatusCode();
string _res = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
_ret = true;
}
}
}
Last edited by Winanjaya; Mar 23rd, 2020 at 03:34 AM.
-
Mar 23rd, 2020, 04:00 AM
#2
Re: How to call another Web API within Web API?
After 750 posts, we shouldn't need to tell you to format your code snippets.
Code:
[HttpPost]
[Route("/B2B/Core_RegistrationAsync")]
[AllowAnonymous]
public async Task<object> Core_RegistrationAsync([FromBody]JObject _jo)
{
try
{
string _company_name = (string)_jo["company_name"];
string _company_email = (string)_jo["company_email"];
string _core_client_id = (string)_jo["core_client_id"];
using (var client = new HttpClient())
{
var url = new Uri(_url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("api_key", Models.AppSettings.Application.PTDigital.B2B_API_KEY);
client.DefaultRequestHeaders.Add("api_secret", Models.AppSettings.Application.PTDigital.B2B_API_SECRET);
var obj = new
{
client_id = _core_client_id,
company_name = _company_name,
company_email = _company_email
};
var json = JsonConvert.SerializeObject(obj);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(_url, content);
HttpStatusCode _statusCode = response.StatusCode;
if (_statusCode == HttpStatusCode.OK)
{
response.EnsureSuccessStatusCode();
string _res = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
_ret = true;
}
}
}
We also should not need to tell you that "it doesn't work" is never an adequate description of a problem. What happens and where does it happen? Are you able to call the same API from another application in the same way? Are you able to call a different API in the same way? Etc, etc. Provide a FULL and CLEAR explanation.
Last edited by jmcilhinney; Mar 23rd, 2020 at 04:03 AM.
-
Mar 23rd, 2020, 04:02 AM
#3
Thread Starter
Fanatic Member
Re: How to call another Web API within Web API?
sorry.. I forgot it..
-
Jul 3rd, 2020, 02:33 AM
#4
Lively Member
Re: How to call another Web API within Web API?
Hello,@Winanjaya
I also went through the same problem, after much research I discovered that the await operator does not stop the work if the HttpClient returns error 500.
To work around the problem I used Task.Wait().
Code:
var response = client.GetAsync ("umbraco/api/Member/Get?username=test");
response.Wait ();
I hope this information will be useful.
Thank you..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|