locked
How to call web api? RRS feed

  • Question

  • User-353733005 posted

    Hi,

    I'm new in web api. I want to call a get method web api which will redirect to the another page which I provide in the query parameter "redirect_uri". In the api have some query parameter. the problem is I don't understand how to call that api with the parameter.

    Here is the sample of the api with query parameter:

    http://127.0.0.1:5000/connect/Check?client_id=webform2&redirect_uri=http://localhost:8080

    Can anyone help me please. 

    Thanks in advance.

    Monday, November 18, 2019 5:56 AM

All replies

  • User1120430333 posted

    HTTPClient() is the defacto.

    https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8

    public List<DtoArticle> GetArticlesByAuthorId(int id)
            {
                var dtoarticles = new List<DtoArticle>();
    
                using (var client = new HttpClient())
                {
                    var uri = new Uri("http://localhost/WebAPI/api/article/GetArticlesByAuthorId?id=" + id);
    
                    var response = client.GetAsync(uri).Result;
    
                    if (!response.IsSuccessStatusCode)
                        throw new Exception(response.ToString());
    
                    var responseContent = response.Content;
                    var responseString = responseContent.ReadAsStringAsync().Result;
    
                    dynamic articles = JArray.Parse(responseString) as JArray;
    
                    foreach (var obj in articles)
                    {
                        DtoArticle dto = obj.ToObject<DtoArticle>();
    
                        dtoarticles.Add(dto);
                    }
                }
    
                return dtoarticles;
            }

    Monday, November 18, 2019 8:01 AM