locked
how to access post web api method RRS feed

  • Question

  • User-1471881183 posted

    hello there,

    i have a post method like below

      [HttpPost]
            public string Group(string strIput)
            {
                string strMessage = string.Empty;
    
    
                try
                {
                    strMessage = m_db.CreateGroup(strIput);
                }
                catch (Exception ex)
                {
                    strMessage = ex.Message;
                }
                return strMessage;
            }

    its working fine from postman, now i want to access this web api method from Test Project so, i did formated url with query string(No Models) so, may i know how to call this Web Api method with formated query string and get back the output message?

    Tuesday, January 12, 2021 5:20 PM

Answers

  • User1686398519 posted

    Hi winseealn, 

    You can use HttpClient to call Web API from .NET client.

    HttpClient client = new HttpClient();
    var testvalue = "1";
    var url = "https://localhost:44343/api/values/group/?strIput=" + testvalue;
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
    HttpResponseMessage response = await client.SendAsync(request);
    if (response.StatusCode == HttpStatusCode.OK)
    {
         var t = response.Content.ReadAsStringAsync().Result;
    }

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 13, 2021 6:24 AM

All replies

  • User475983607 posted

    What it your test application?  JavaScript?  C#?

    Tuesday, January 12, 2021 6:58 PM
  • User1686398519 posted

    Hi winseealn, 

    You can use HttpClient to call Web API from .NET client.

    HttpClient client = new HttpClient();
    var testvalue = "1";
    var url = "https://localhost:44343/api/values/group/?strIput=" + testvalue;
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
    HttpResponseMessage response = await client.SendAsync(request);
    if (response.StatusCode == HttpStatusCode.OK)
    {
         var t = response.Content.ReadAsStringAsync().Result;
    }

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 13, 2021 6:24 AM