Fix: Error on posting data using HttpClient

 Last week, I’s spending some time exploring OneDrive Rest API. While trying to send a POST request for receiving access token, I got a error as - “{"error":"invalid_request","error_description":"The provided request must have content-type 'application/x-www-form-urlencoded'."}”. 

I modified my code further. This time, I got another exception – “Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.” Finally, I figured out the correct way to set the content-type header while sending a POST request using HTTPClient. And, here it is.

HttpClient client = new HttpClient();
StringContent content = new StringContent(BodyContent, Encoding.UTF8, "application/x-www-form-urlencoded");
var responseMessage = await client.PostAsync(Uri, content);

Phew! Hope this helps.


Non-numeric values for enumeration using Attributes in c#

In this post, I’ll explain how you can provide string values for an enum. Usually, we’ll  assign integer (by default), long or as a bit field using FlagAttribute.

For assigning a string value first you need to create a class inherited from Attribute class. 


Once your attribute class is ready. You can use the attribute and your string values as below: 



Now, for accessing the enum string value and converting the string values to respective enum , it’s good to have an extension so that you can convert back and forth seamlessly. 


Now, you are fully occupied for using the enum and its custom values. Below is an example on how to use it. 



 Hope this helps.


Zoom bug in Windows Phone 8.1 Developer Preview

This is the first time, I’m installing a Windows Phone Developer Preview version. Rest of the time, I always wait until the final version is pushed OTA. This time I’m little hungry because of “Cortana”. The iconic stuff that made we to break the wait and install the dev preview.

Last week, surprising, I encountered an unusual bug in the dev preview (thrice, if I could remember) and I call it as “Zoom Bug”. Ya, entire screens got zoomed somehow – start screen, app screen, multi-tasking screen and even the lock screen. Take a look for yourselves. 

WP_000995   WP_000996

WP_000997   WP_000998

WP_000999   WP_001000

And, how did I zoom out of the device? I restarted the device! Smile