<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web API And HttpClient &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/web-api-and-httpclient/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Sun, 15 Aug 2021 14:42:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Web API And HttpClient &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Web API With HttpClient Or Consume Web API From Console Application</title>
		<link>https://www.sibeeshpassion.com/web-api-with-httpclient-or-consume-web-api-from-console-application/</link>
					<comments>https://www.sibeeshpassion.com/web-api-with-httpclient-or-consume-web-api-from-console-application/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Mon, 28 Mar 2016 00:00:37 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Web API]]></category>
		<category><![CDATA[AspNet]]></category>
		<category><![CDATA[HttpClient]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Web API And HttpClient]]></category>
		<category><![CDATA[Web API In Console Application]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=11424</guid>

					<description><![CDATA[In this article, we are going to learn how we can call Web API using HttpClient. Normally we call a Web API either from a jQuery Ajax or from Angular JS right? Recently I came across a need of calling our Web API from the server-side itself. Here I am going to use two Visual Studio applications. One is our normal Web API application in which I have a Web API controller and actions, another one is a console application where I consume my Web API. Sounds good? I am using Visual Studio 2015. You can always get the tips/tricks/blogs [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In this article, we are going to learn how we can call Web API using HttpClient. Normally we call a Web API either from a jQuery Ajax or from Angular JS right? Recently I came across a need of calling our Web API from the server-side itself. Here I am going to use two Visual Studio applications. One is our normal Web API application in which I have a Web API controller and actions, another one is a console application where I consume my Web API. Sounds good? I am using Visual Studio 2015. You can always get the tips/tricks/blogs about these mentioned technologies from the links given below.</p>



<ul class="wp-block-list"><li><a href="http://sibeeshpassion.com/category/mvc/" target="_blank" rel="noopener">MVC Tips, Tricks, Blogs</a></li><li><a href="http://sibeeshpassion.com/category/web-api/" target="_blank" rel="noopener">Web API Tips, Tricks, Blogs</a></li></ul>



<p>Now we will go and create our application. I hope you will like this.</p>



<p><strong>Download the source code</strong></p>



<p>You can always download the source code here.</p>



<ul class="wp-block-list"><li><a href="https://github.com/SibeeshVenu/WebAPI-HttpClient" target="_blank" rel="noreferrer noopener">Web API Application and Consumer</a></li></ul>



<p><strong>Background</strong></p>



<p>We all use Web API in our applications to implement HTTP services. HTTP services are much simpler than ever if we use Web API. But the fact is, the benefits of a Web API is not limited to that. Previously we use WCF services instead of Web API, where we were working with endpoints and all. Here I am going to explain an important feature of a Web API that we can call Web API from our server itself instead of using an Ajax Call. That is pretty cool, right? Now we will create our application.</p>



<p>First, we will create our Web API application.</p>



<p><strong>Creating Web API application</strong></p>



<p>Click File-&gt; New-&gt; Project then select MVC application. We will select the template as empty from the following pop-up and select the core references and folders for MVC.</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/03/Empty-Template-With-MVC-And-Web-API-Folders-e1458711950206.png"><img fetchpriority="high" decoding="async" width="650" height="484" src="http://sibeeshpassion.com/wp-content/uploads/2016/03/Empty-Template-With-MVC-And-Web-API-Folders-e1458711950206.png" alt="Empty Template With MVC And Web API Folders" class="wp-image-11405" srcset="/wp-content/uploads/2016/03/Empty-Template-With-MVC-And-Web-API-Folders-e1458711950206.png 650w, /wp-content/uploads/2016/03/Empty-Template-With-MVC-And-Web-API-Folders-e1458711950206-300x223.png 300w, /wp-content/uploads/2016/03/Empty-Template-With-MVC-And-Web-API-Folders-e1458711950206-400x298.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><figcaption>Empty Template With MVC And Web API Folders</figcaption></figure>



<p>Once you click OK, a project with MVC like folder structure with core references will be created for you.</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/03/Folder-Structure-And-References-For-Empty-MVC-Project.png"><img decoding="async" width="267" height="367" src="http://sibeeshpassion.com/wp-content/uploads/2016/03/Folder-Structure-And-References-For-Empty-MVC-Project.png" alt="Folder Structure And References For Empty MVC Project" class="wp-image-11362" srcset="/wp-content/uploads/2016/03/Folder-Structure-And-References-For-Empty-MVC-Project.png 267w, /wp-content/uploads/2016/03/Folder-Structure-And-References-For-Empty-MVC-Project-218x300.png 218w" sizes="(max-width: 267px) 100vw, 267px" /></a><figcaption>Folder Structure And References For Empty MVC Project</figcaption></figure>



<p><strong>Using the code</strong></p>



<p>We will set up our database first so that we can create an Entity Model for our application later.</p>



<p><strong>Create a database</strong></p>



<p>The following query can be used to create a database in your SQL Server.</p>



<script src="https://gist.github.com/SibeeshVenu/5c2eef73bb0c3a856cb538333543d0c6.js"></script>



<p>Now we will create the table we needed. As of now, I am going to create the table <em>tblTags </em></p>



<p><strong>Create tables in the database</strong></p>



<p>Below is the query to create the table <em>tblTags</em>.</p>



<script src="https://gist.github.com/SibeeshVenu/2673dc6b515dde9485790d7fb180dc69.js"></script>



<p>Can we insert some data into the tables now?</p>



<p><strong>Insert data to the table</strong></p>



<p>You can use the below query to insert the data to the table <em>tblTags</em></p>



<script src="https://gist.github.com/SibeeshVenu/6bfd40c9b96b415d07db6269593a6050.js"></script>



<p>The next thing we are going to do is creating an ADO.NET Entity Data Model.</p>



<p><strong>Create Entity Data Model</strong></p>



<p>Right-click on your model folder and click new, select ADO.NET Entity Data Model. Follow the steps given. Once you have done the processes, you can see the edmx file and other files in your model folder. Here I gave Dashboard for our Entity data model name. Now you can see a file with an edmx extension has been created.<br>Now will create our Web API controller.</p>



<p><strong>Create Web API Controller</strong></p>



<p>To create a Web API controller, just right click on your controller folder and click Add -&gt; Controller -&gt; Select Web API 2 controller with actions, using Entity Framework.</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web-API-2-Controller-With-Actions-Using-Entity-Framework-e1458709497551.png"><img decoding="async" width="650" height="448" src="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web-API-2-Controller-With-Actions-Using-Entity-Framework-e1458709497551.png" alt="Web API 2 Controller With Actions Using Entity Framework" class="wp-image-11401" srcset="/wp-content/uploads/2016/03/Web-API-2-Controller-With-Actions-Using-Entity-Framework-e1458709497551.png 650w, /wp-content/uploads/2016/03/Web-API-2-Controller-With-Actions-Using-Entity-Framework-e1458709497551-300x207.png 300w, /wp-content/uploads/2016/03/Web-API-2-Controller-With-Actions-Using-Entity-Framework-e1458709497551-160x110.png 160w, /wp-content/uploads/2016/03/Web-API-2-Controller-With-Actions-Using-Entity-Framework-e1458709497551-400x276.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><figcaption>Web API 2 Controller With Actions Using Entity Framework</figcaption></figure>



<p>Now select <em>tblTag (WebAPIWithHttpClient.Models)</em> as our Model class and <em>TrialsDBEntities (WebAPIWithHttpClient.Models)</em> as data context class. This time we will select controller with async actions.</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web-API-Controller-With-Async-Actions.png"><img decoding="async" width="593" height="233" src="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web-API-Controller-With-Async-Actions.png" alt="Web API Controller With Async Actions" class="wp-image-11425" srcset="/wp-content/uploads/2016/03/Web-API-Controller-With-Async-Actions.png 593w, /wp-content/uploads/2016/03/Web-API-Controller-With-Async-Actions-300x118.png 300w, /wp-content/uploads/2016/03/Web-API-Controller-With-Async-Actions-400x157.png 400w" sizes="(max-width: 593px) 100vw, 593px" /></a><figcaption>Web API Controller With Async Actions</figcaption></figure>



<p>As you can see It has been given the name of our controller as <em>tblTags</em>. Here I am not going to change that, if you wish to change, you can do that.</p>



<p>Now you will be given the following codes in our new Web API controller.</p>



<script src="https://gist.github.com/SibeeshVenu/d47bd69da2de534bd9f53224e464bb76.js"></script>



<p>As you can see, we have actions for,</p>



<ul class="wp-block-list"><li>Get</li><li>Post</li><li>Put</li><li>Delete</li></ul>



<p>So the coding part to fetch the data from the database is ready, now we need to check whether our Web API is ready for action!. To check that, you just need to run the URL <em>http://localhost:7967/api/tbltags</em>. Here <em>tblTags</em> is our Web API controller name. I hope you get the data as a result.</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web_API_Result-e1458733092316.png"><img decoding="async" width="650" height="342" src="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web_API_Result-e1458733092316.png" alt="Web_API_Result" class="wp-image-11413" srcset="/wp-content/uploads/2016/03/Web_API_Result-e1458733092316.png 650w, /wp-content/uploads/2016/03/Web_API_Result-e1458733092316-300x158.png 300w, /wp-content/uploads/2016/03/Web_API_Result-e1458733092316-600x315.png 600w, /wp-content/uploads/2016/03/Web_API_Result-e1458733092316-400x210.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><figcaption>Web_API_Result</figcaption></figure>



<p>As of now, our Web API application is ready, and we have just tested whether it is working or not. Now we can move on to create a console application where we can consume this Web API with the help of HttpClient. So shall we do that?</p>



<p><strong>Create Console Application To Consume Web API</strong></p>



<p>To create a console application, Click File -&gt; New -&gt; Click Windows -&gt; Select Console application -&gt; Name your application -&gt; OK</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/03/Console-Application-e1458836504170.png"><img decoding="async" width="650" height="396" src="http://sibeeshpassion.com/wp-content/uploads/2016/03/Console-Application-e1458836504170.png" alt="Console Application" class="wp-image-11426" srcset="/wp-content/uploads/2016/03/Console-Application-e1458836504170.png 650w, /wp-content/uploads/2016/03/Console-Application-e1458836504170-300x183.png 300w, /wp-content/uploads/2016/03/Console-Application-e1458836504170-400x244.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><figcaption>Console Application</figcaption></figure>



<p>I hope now you have a class called Program. cs with the below codes.</p>



<script src="https://gist.github.com/SibeeshVenu/bbbe25768fa7a0f6ee294a2e7ef794a3.js"></script>



<p>Now we will start our coding, We will create a class called <em>tblTag </em>with some properties so that we can use those when we need them.</p>



<script src="https://gist.github.com/SibeeshVenu/ad2e7b0da0ad725a19777b76a2f55d5c.js"></script>



<p>To get started using the class <em>HttpClient</em>, you must import the namespace as follows.</p>



<pre class="wp-block-code"><code>using System.Net.Http;</code></pre>



<p>Once you have imported the namespaces, we will set our HttpClient and the properties as follows.</p>



<script src="https://gist.github.com/SibeeshVenu/fe0d1863a944dddb528ecff8d376cf76.js"></script>



<p>As you can see we are just giving the base address of our API and setting the response header. Now we will create an async action to get the data from our database by calling our Web API.</p>



<p><strong>Get operation using HttpClient</strong></p>



<pre class="wp-block-code"><code>MyAPIGet(cons).Wait();</code></pre>



<p>Following is the definition of the <em>MyAPIGet </em>function.</p>



<script src="https://gist.github.com/SibeeshVenu/79b3004ee97c61d112100e8324a0e7fb.js"></script>



<p>Here <em>res.EnsureSuccessStatusCode();</em> ensure that it throws errors if we get any. If you don&#8217;t need to throw the errors, please remove this line of code. If the async call is a success, the value in <em>IsSuccessStatusCode</em> will be true.</p>



<p>Now when you run the above code, there are chances to get an error as follows.</p>



<p><em>Error CS1061 &#8216;HttpContent&#8217; does not contain a definition for &#8216;ReadAsAsync&#8217; and no extension method &#8216;ReadAsAsync&#8217; accepting a first argument of type &#8216;HttpContent&#8217; could be found (are you missing a using directive or an assembly reference?)</em></p>



<p>This is just because the <em>ReadAsAsync</em> is a part of <em>System.Net.Http.Formatting.dll</em> which we have not added to our application as a reference yet. Now we will do that? Sounds OK?</p>



<p>Just right click on the references and click add reference -&gt; Click browse -&gt; search for <em>System.Net.Http.Formatting.dll</em> &#8211; Click OK</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/03/Add-References-e1458838521697.png"><img decoding="async" width="650" height="447" src="http://sibeeshpassion.com/wp-content/uploads/2016/03/Add-References-e1458838521697.png" alt="Add References" class="wp-image-11427" srcset="/wp-content/uploads/2016/03/Add-References-e1458838521697.png 650w, /wp-content/uploads/2016/03/Add-References-e1458838521697-300x206.png 300w, /wp-content/uploads/2016/03/Add-References-e1458838521697-160x110.png 160w, /wp-content/uploads/2016/03/Add-References-e1458838521697-400x275.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><figcaption>Add References</figcaption></figure>



<p>Please add <em>Newtonsoft.Json</em> also. Now let us run our project and see our output.</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web_API_Consumer_Get_Output1-e1458882223957.png"><img decoding="async" width="650" height="328" src="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web_API_Consumer_Get_Output1-e1458882223957.png" alt="Web_API_Consumer_Get_Output" class="wp-image-11429" srcset="/wp-content/uploads/2016/03/Web_API_Consumer_Get_Output1-e1458882223957.png 650w, /wp-content/uploads/2016/03/Web_API_Consumer_Get_Output1-e1458882223957-300x151.png 300w, /wp-content/uploads/2016/03/Web_API_Consumer_Get_Output1-e1458882223957-400x202.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><figcaption>Web_API_Consumer_Get_Output</figcaption></figure>



<p>Now shall we create a function for updating the record? Yes, we are going to create a function with the &#8216;Put&#8217; request. Please copy and paste the preceding code for that.</p>



<p><strong>Put operation using HttpClient</strong></p>



<script src="https://gist.github.com/SibeeshVenu/bdbfdf46d94b59f919feb5e55dd5b75b.js"></script>



<p>As you can see, we are updating the record as below once we get the response from <em>await cons.GetAsync(&#8220;api/tblTags/2&#8221;)</em> .</p>



<script src="https://gist.github.com/SibeeshVenu/93b430884a048b02120aab726593212a.js"></script>



<p>Again, run your application and check whether the tag name has been changed to &#8216;New Tag&#8217;.</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web_API_Consumer_Put_Output-e1458882875423.png"><img decoding="async" width="650" height="328" src="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web_API_Consumer_Put_Output-e1458882875423.png" alt="Web_API_Consumer_Put_Output" class="wp-image-11430" srcset="/wp-content/uploads/2016/03/Web_API_Consumer_Put_Output-e1458882875423.png 650w, /wp-content/uploads/2016/03/Web_API_Consumer_Put_Output-e1458882875423-300x151.png 300w, /wp-content/uploads/2016/03/Web_API_Consumer_Put_Output-e1458882875423-400x202.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><figcaption>Web_API_Consumer_Put_Output</figcaption></figure>



<p>Now did you see that your tag name has been changed? If yes, we are ready to go for our next operation. Are you ready?</p>



<p><strong>Delete operation using HttpClient</strong></p>



<p>We will follow the same procedure for delete operation too. Please see the code for the delete operation below.</p>



<script src="https://gist.github.com/SibeeshVenu/84ff6853fec53f7b93345170be8050bd.js"></script>



<p>To delete a record we uses <em> res = await cons.DeleteAsync(&#8220;api/tblTags/2&#8221;);</em> method. Now run your application and see the result.</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web_API_Consumer_Delete_Output-e1458883191973.png"><img decoding="async" width="650" height="328" src="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web_API_Consumer_Delete_Output-e1458883191973.png" alt="Web_API_Consumer_Delete_Output" class="wp-image-11431" srcset="/wp-content/uploads/2016/03/Web_API_Consumer_Delete_Output-e1458883191973.png 650w, /wp-content/uploads/2016/03/Web_API_Consumer_Delete_Output-e1458883191973-300x151.png 300w, /wp-content/uploads/2016/03/Web_API_Consumer_Delete_Output-e1458883191973-400x202.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><figcaption>Web_API_Consumer_Delete_Output</figcaption></figure>



<p>What action is pending now? Yes, it is Post.</p>



<p><strong>Post-operation using HttpClient</strong></p>



<p>Please add the below function to your project for the post-operation.</p>



<script src="https://gist.github.com/SibeeshVenu/b52bb82946ad305c5fa95bc1ccbe6ae8.js"></script>



<p>We are just creating a new tblTag and assign some values, once the object is ready we are calling the method PostAsJsonAsync as follows.</p>



<script src="https://gist.github.com/SibeeshVenu/4f73e7e9ee9355afb808e0bbbe00647c.js"></script>



<p>As you have noticed, I have not provided the <em>tagId</em> in the object, do you know why? I have already set Identity Specification with Identity Increment 1 in my table tblTags in the SQL database.</p>



<p>Now we will see the output. Shall we?</p>



<figure class="wp-block-image alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web_API_Consumer_Post_Output-e1458883741126.png"><img decoding="async" width="650" height="328" src="http://sibeeshpassion.com/wp-content/uploads/2016/03/Web_API_Consumer_Post_Output-e1458883741126.png" alt="Web_API_Consumer_Post_Output" class="wp-image-11432" srcset="/wp-content/uploads/2016/03/Web_API_Consumer_Post_Output-e1458883741126.png 650w, /wp-content/uploads/2016/03/Web_API_Consumer_Post_Output-e1458883741126-300x151.png 300w, /wp-content/uploads/2016/03/Web_API_Consumer_Post_Output-e1458883741126-400x202.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><figcaption>Web_API_Consumer_Post_Output</figcaption></figure>



<p>We have done everything!. That&#8217;s fantastic right? Have a happy coding.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>You can always use WebClient also for this requirement, that I will share in an another article.</p></blockquote>



<p><strong>Conclusion</strong></p>



<p>Did I miss anything that you may think is needed? Did you try Web API yet? Have you ever wanted to call a Web API from the server itself or any console application? Could you find this post useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>



<p><strong>Your turn. What do you think?</strong></p>



<p>A blog isn&#8217;t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>



<p>Kindest Regards<br>Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/web-api-with-httpclient-or-consume-web-api-from-console-application/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
	</channel>
</rss>
