How to make a post request using json object without using restsharp?
How many times have you said to yourself, create a project by using ready made extensions like RestSharp and other Microsoft Visual Studio Extension using Nuget Package Manager.But everytime i use them they let me down. Why?.The reason is every time there is a known bug in those pre written modules or may be you will end up with future bugs which will demolish your programming world and give you no clue.
My first encounter
Last month i had the same experience. I wanted to call an api which required me to post a json object on the server. Guess what, a bit of researching on google leaded me to RestSharp and it gave me happiness .
So all i need to do was
RestClient rc=new RestClient();
This seemed familiar and i was good to go. I made couple of get requests with it and which worked fine. But when i made my first post request it was setting the request.format to "application/x-www-form-urlendcoded" however i wanted it to go as "application/json".I set the format of request object still it was changing it back to "x-www-form-urlencoded". I was really frustrated.
Why ? i researched on Google a bit and you know what, it was known bug listed on stackoverflow.
http://stackoverflow.com/questions/17815065/set-content-type-header-using-restsharp
Well do i care? No.
Damage was done
So finally i realized so far the methods i already wrote on the bases of RestSharp were useless.
Now it was the time i had to write my own client.
Mission Client
So i started with default httpclient
Using (var client=new HttpClient()){
client.BaseAddress = new Uri("http://www.someapi.com/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
The 3rd line
.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
does the magic for you .
.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
does the magic for you .
Now further i made a serialisable json object and added data contract on the class and data member and made sure the property names i used should be same as my json name value required.for instance:-
if i want to send student object i may want a object
in my service provider class where i made my httpclient i can simply say this
Advert:
Add blog to blog directory at OnToplist.com.
------------------------------------------------------------------------------------------------------------
client.PostAsJsonAsync("v1/" + resources + "/?" + "username=" + id + "&" + "api_key=" + key, new Student(){ name="Riju"}).Result;
Boom this worked ! So this was a simple walkthrough if you are unable to use RestSharp for post request you can simple write your own class to make httpclient request which is a better solution.
However you can write more properties to set the resources and parameters for rest api or you can use fluent pattern that helps you to set properties and returns the same instance . Ta! Da! you will have your own RestSharp library .Now create a dll or distribute it in-house. This way you don't need to have any dependency on outer code and your application will be more easily managed .
[DataContract]
class Student{
[DataMember]public string name{ get; set;}
}
in my service provider class where i made my httpclient i can simply say this
Advert:
Add blog to blog directory at OnToplist.com.
------------------------------------------------------------------------------------------------------------
client.PostAsJsonAsync("v1/" + resources + "/?" + "username=" + id + "&" + "api_key=" + key, new Student(){ name="Riju"}).Result;
Boom this worked ! So this was a simple walkthrough if you are unable to use RestSharp for post request you can simple write your own class to make httpclient request which is a better solution.
However you can write more properties to set the resources and parameters for rest api or you can use fluent pattern that helps you to set properties and returns the same instance . Ta! Da! you will have your own RestSharp library .Now create a dll or distribute it in-house. This way you don't need to have any dependency on outer code and your application will be more easily managed .
Riju Vashisht Developer Toronto |