Reputation: 583
I'm trying to call a POST method on my WCF service, from a website. How ever, the WCF service complains the json string is not valid.
WCF Service method:
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "reserve/batteries", BodyStyle = WebMessageBodyStyle.Bare)]
public bool ReserveBatteries(ReserveModel values)
Method calling the POST, from the website:
var stations = TempData["Stations"];
string jsonStations = JsonHelper.SerializeJson(stations);
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://localhost:49288/reserve/batteries");
UTF8Encoding encoding = new UTF8Encoding();
string postData = "{\"Stations\": " + jsonStations;
postData += ", \"User\": \"" + User.Identity.Name + "\" }";
byte[] data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/json; charset=utf-8";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
I am using Newtonsoft json libary, to serialize. The json string I am trying to send to my service are:
{
"Stations": [
{
"BatteryStorages": null,
"CreatedDate": "2013-06-06T00:00:00+02:00",
"Description": "Aalborg City",
"Edges": [],
"ID": 3,
"IsActive": true,
"IsOperational": true,
"Reservations": [],
"StationLat": 57.02881,
"StationLong": 9.91777,
"StationMaintenances": [],
"StationType": null,
"Title": "Aalborg",
"TypeId": 1,
}
],
"User": "user1"
}
I have validated this with JsonLint and it says it's a valid string.
Unvalid string (according to JsonLint), which WCF accepts:
{
"Stations": "[
{
BatteryStorages:null,
CreatedDate:\"/Date(1370469600000+0200)/\",
Description:\"Aalborg City\",
Edges:[],
ID:\"3\",
IsActive:\"true\",
IsOperational:\"true\",
Reservations:[],
StationLat:\"57.02881\",
StationLong:\"9.91777\",
StationMaintenances:[],
StationType:null,
Title:\"Aalborg\",
TypeId:\"1\"
}
]",
"User": "user1"
}
The exception thrown from WCF:
There was an error deserializing the object of type RestfulAPI.Resources.ReserveModel. End element 'Stations' from namespace '' expected. Found element 'item' from namespace ''.
How can this be? Why are WCF complaining it's not valid when jsonlint (and others) say it is.
Edit:
[DataContract]
public class ReserveModel
{
[DataMember]
public string Stations { get; set; }
[DataMember]
public string User { get; set; }
}
Upvotes: 3
Views: 883
Reputation: 264
The problem is that your Model objects are not in the correct format.
To create the correct JSON classes from your JSON you could use json2csharp or if you have, for example VS2012 update 2, you could copy your JSON and paste the JSON as classes (Edit -> Paste Special > Paste JSON as classes).
You will have to remember that for example DateTime objects have to be delivered in a certain way WCF supports. More information on this can be found here (msdn - WCF JSON serialization). To dodge this problem you can just type the variable to string and convert it at a later time.
Hope this helps!
Upvotes: 5