Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to call another Web API within Web API?

Winanjaya

New member
Joined
Nov 12, 2003
Messages
757
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:
Joined
May 20, 2005
Messages
104,556
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:
Top