How to consume RESTful APi from server side code - C#

I was working on a project where I got a Web API URL with parameters, I was asked to consume this API and get the response from there.


What is RESTful Service?


Representation State Transfer. REST says each resource should be accessed by URI. In REST client sends a request and server responds for that request. This response is the representation of a resource which can be in HTML, XML, JSON, Plain Text format. As we know REST is a style, so it can use any protocol like HTTP, SOAP. REST is stateless, cacheable. To read more about REST Click Here.

REST HTTP Verbs

REST uses 4 HTTP verbs.
  • GET
  • POST
  • PUT 
  • DELETE
Remember, REST is an architectural style so it is a platform independent pattern and it is not tightly coupled with a technology.
You can create a RESTful service in WCF and WEB API as well. See my another blog on  "How to create a RESTful service in WCF?"

In this blog, I will show you, how to consume an API using C# code

How to call an API from C# code?

To call an API, I got below details:
  • API URL
  • Parameters
  • And response code eg 200 for ok, 404 for not found etc.
I did the same implementation using JQuery but fail, then I decided to use C# code.
Here is the code which will consume the Web API and will get a response from there.
This is 100% working code.


private static void CreateObject()
        {
          
             
            string URL = "<Rest Service URL>";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "application/json";
           //request.ContentLength = DATA.Length;
            StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
            requestWriter.Write(DATA);
            requestWriter.Close();

            try
            {
                WebResponse webResponse = request.GetResponse();
                Stream webStream = webResponse.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string response = responseReader.ReadToEnd();
                Console.Out.WriteLine(response); //Get Web response and proceed accordingly
                responseReader.Close();
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("-----------------");
                Console.Out.WriteLine(e.Message);
            }

        }
    }


Protected by Copyscape

Comments

  1. i was trying to call a rest api using ajax post with json and i was getting error.
    Now your code solved my issue. Thanks and keep blogging.

    ReplyDelete

Post a Comment

Dear Readers, Please post your valuable feedback in the comment section if you like this blog or if you have any suggestions. I would love to hear the same from you. Thanks

Popular

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX

What is Cloud Computing - A guide for beginners