Home > Software design >  API Works in Postman but while hitting from code no response is generated
API Works in Postman but while hitting from code no response is generated

Time:08-10

I have Get API for getting data, when it is hitting from Postman it gives response properly, but while hitting from the code it skipping the code. I am using HttpClient(). My code for hitting the api is below :

I didn't get the point here why this happening.

public async Task<string> GetAsync(string uri, string serverAddress = "")
{
    try
    {
        string id = System.DateTime.Now.ToString("hhmmss"); 
        HttpClient httpClient = new HttpClient();
        httpClient.Timeout = new TimeSpan(0, 0, 0, 0, Timeout);
        httpClient.BaseAddress = new Uri(serverAddress);
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
                
        var responseMessage = await httpClient.GetAsync(new Uri(uri));

        var responseContent = await responseMessage.Content.ReadAsStringAsync();

        if (!responseMessage.IsSuccessStatusCode)
        {
            throw new RestException
            {
                ResponseCode = (int)responseMessage.StatusCode,
                ResponseContent = responseContent
            };
        }

        return responseContent;
    }
    catch (Exception ex)
    {
        throw new Exception($"The Get request timed out. Exception :- {ex.Message}");
    }
}

From this line -->

var responseMessage = await httpClient.GetAsync(new Uri(uri));

my code is skipping

Edit -->

After Testing more, I also got no exception after the timeout.

CodePudding user response:

comment below line in your code

httpClient.BaseAddress = new Uri(serverAddress);

and pass full url instead of new Uri(uri) in below line

var responseMessage = await httpClient.GetAsync(new Uri(uri));

CodePudding user response:

Try the following:

public async Task<string> GetAsync(string uri, string serverAddress = "")
    {
        try
        {
            string id = System.DateTime.Now.ToString("hhmmss");
            HttpClient httpClient = new HttpClient();
            httpClient.Timeout = new TimeSpan(0, 0, 0, 0, Timeout);
            httpClient.BaseAddress = new Uri(serverAddress);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

            var responseMessage = await httpClient.GetAsync(uri);

            var responseContent = await responseMessage.Content.ReadAsStringAsync();

            if (!responseMessage.IsSuccessStatusCode)
            {
                throw new RestException
                {
                    ResponseCode = (int)responseMessage.StatusCode,
                    ResponseContent = responseContent
                };
            }

            return responseContent;
        }
        catch (Exception ex)
        {
            throw new Exception($"The Get request timed out. Exception :- {ex.Message}");
        }
    }

Ensure you add await before calling this method in your code and check if the full URL that's actually called has been build correctly. You can just pass a string into the GetAsync() method, no need to create a new URI.

  •  Tags:  
  • c#
  • Related