Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm trying to implement a method which will have contentType: 'application/x-www-form-urlencoded' which will be expecting json string as an input.

Sample Json request -
{
'amount': '2500.00',
'buyer': 'foo@example.com',
'buyer_name': 'John Doe',
'buyer_phone': '9999999999',
'currency': 'INR',
'fees': '125.00',
'longurl': 'https://www.instamojo.com/@dalan/264a89803cec4401862f01e28f720eca',
'mac': '1ddf3b78f84d071324c0bf1d3f095898267d60ee',
'payment_id': 'MOJO5a06005J21512197',
'payment_request_id': '264a89803cec4401862f01e28f720eca',
'purpose': 'FIFA 16',
'shorturl': 'https://imjo.in/NNNXn',
'status': 'Credit'
}

Currently I have implemented the method which perfectly works fine for application/json but it is not working for contentType: 'application/x-www-form-urlencoded'
I want to make this workable for contentType: 'application/x-www-form-urlencoded'

Could anyone please provide any pointers for the same
Thank you
Posted

1 solution

When you use contentType as "application/x-www-form-urlencoded", it appends append value to url. This is the default value of contentType. It looks like:
C#
?Name=Manas&Age=28

But when you use contentType as "application/json", it looks like
C#
{ Name : 'Manas', Age: 28}

So "application/x-www-form-urlencoded" is not same as "application/json".

Try with below code:
C#
public class MyCustomClass {
    public string amount{ get; set; }
    public string buyer{ get; set; }
}

Next use .net JavaScriptSerializer().Deserialize to get data .Net custom object.
C#
[HttpPost]
public void Post([FromBody]string formData){
    MyCustomClass obj = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<mycustomclass>(formData);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900