Results 1 to 4 of 4

Thread: How to call another Web API within Web API?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    782

    Question 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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    782

    Re: How to call another Web API within Web API?

    sorry.. I forgot it..

  4. #4
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    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
  •  



Click Here to Expand Forum to Full Width