Monday, May 1, 2017

How to Post JSON data with WebRequest in .NET

Hello,
There was a question on how to post a JSON data using the WebRequest class given that using Ajax, the post data would look like this.
data: '{ "age": "78", "weight": "51" }'
In .NET, you need to store the JSON data into a string object and escape those double quotes before passing it to the WebRequest object.
C#.NET
string postData = "{ \"age\": \"78\", \"weight\": \"51\" }";
VB.NET
Dim postData As String = "{ ""age"": ""78"", ""weight"": ""51"" }"

Cheers! :-)

0 comments:

Post a Comment