dcsimg
Results 1 to 3 of 3

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

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    757

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,120

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    757

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

    sorry.. I forgot it..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width