Http Post Request in C# With Authorization Header

Image from https://www.linkedin.com/learning/learning-c-sharp-3

I recently worked on an Asp.Net Core project and had to make a couple of HTTP requests to a particular API endpoint due to a whole range of systems that needed to be integrated with each other for some reason.

Most of the code snippets or solutions I have seen especially when making a simple Post Request tend to be scattered with repeating segments especially when it has to do with adding Authorization headers as well as Content-type, along with the data that is to be sent to the URI endpoint.

Quick Start

We’ll be making use of WebRequest, which is an abstract class defined in the System.Net namespace, Newtonsoft which is used to Serialize and deserialize any .NET Core object with Json.NET’s powerful JSON serializer.

Now let’s see how we can write a simple but effective method, for making a Post request in C#.

Ok, let's walk through the above code snippet.

We have a method defined as PostAsync, which takes in three parameters i.e token, URL, and content. Note that this content can be of any object type.

Next, we have the request definitions provided by the WebRequest class, You use the Create method of WebRequest to create an instance of WebRequest.

To actually make the post request we need to assign the request method to a string of “POST”, and notice the request.Headers.Add method, which takes in a name-value pair param. In this case, we are using an Authorization Header which requires an access token. Simple right?

Now Let’s see the part where we make the actual request, which also determines the response we get.

Notice the try-catch statements, the try statement allows you to define a block of code to be tested for errors while it is being executed, while the catch statement allows you to define a block of code to be executed if an error occurs in the try block.

In this case, we are expecting different responses, First is the actual response, which is expected when the request is made, then if anything goes wrong, we get an exception and its response as well.

Finally, the Newtonsoft JSON package is used to deserialize these responses, to an actual object. Note that this varies base on our use cases.

Conclusion

We now see how easy it is to make a post request in c#, adding the content-type, authorization header, and the content itself.

I tried as much as I can to make this readable and concise, so if you have any questions or corrections, I’ll be glad to know.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Deploying a Docker Container on EC2

Flutter Foundation - Going from setState to Architecture (Handling Async Behaviour)

Lenovo P2 OEM Unlock

Secure Code Note: OWASP Top 10 #1 Injections and Jave EE

Setting up WSO2 sample OAuth applications with WSO2 Identity Server

OXYGEN BUILDER REVIEW: A COMPLETE PAGE BUILDER

What makes OSYS different?

Multi-Cloud Database Management

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CHIBUIKE KENNETH

CHIBUIKE KENNETH

More from Medium

11 important .Net Development Tools for Smarter Development | iFour Technolab

11 important .Net Development Tools for Smarter Development | iFour Technolab

AWS SDK + .NET Core + S3 | Part 1

Minimal APIs in .NET 6: how to use it to enrich data for analytics — Part2

Write a code to retrieve data from table into a datagridview in CSharp