I have created a webservice in C# which looks like this:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string UpdateHeatCallJSON(string json)
{
HeatItem item = JsonConvert.DeserializeObject<HeatItem>(json);
UpdateHeatCall(item);
HeatItemResponse response = new HeatItemResponse();
//... more code
return JsonConvert.SerializeObject(response);
}
I basically have an object HeatItem, which I want to pass in as an argument.
I currently consume the web service (for testing) in a C# console application and having challenges finding the correct format. This is my call:
static void UpdateHeatItemJSON()
{
// corrected to WebRequest from HttpWebRequest
WebRequest request = WebRequest.Create(requestServer + "/UpdateHeatCallJSON");
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
string postData = "";
postData = @"{'json':'{""BusinessPartner"":""00000000-0000-0000-0000-000000000000"",""CaseNumber"":4,""CaseDescription"":""first case"",""CaseType"":"""",""CaseSeverity"":"""",""DueDate"":""0001-01-01T00:00:00"",""AssignmentNumber"":5,""AssignmentDescription"":"""",""AssignmentCreation"":""0001-01-01T00:00:00"",""AssignmentTime"":""0001-01-01T00:00:00"",""ChangeDate"":""0001-01-01T00:00:00"",""ChangeTime"":""0001-01-01T00:00:00"",""Group"":"""",""SubGroup"":"""",""Module"":"""",""AssignmentStatus"":"""",""KPIChallenge"":false,""KPI1Status"":"""",""KPI1User"":"""",""KPI1Date"":""0001-01-01T00:00:00"",""KPI1Time"":""0001-01-01T00:00:00"",""KPI2Status"":"""",""KPI2User"":"""",""KPI2Date"":""0001-01-01T00:00:00"",""KPI2Time"":""0001-01-01T00:00:00"",""SessionID"":null}'}";
System.Diagnostics.Debug.Print(postData);
//get a reference to the request-stream, and write the postData to it
using (Stream s = request.GetRequestStream())
{
using (StreamWriter sw = new StreamWriter(s))
sw.Write(postData);
}
//get response-stream, and use a streamReader to read the content
using (Stream s = request.GetResponse().GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
string jsonData = sr.ReadToEnd();
JObject jObject = JObject.Parse(jsonData);
JToken jHeatItem = jObject["HeatItem"];
}
}
}
Looking at postData above, formatting the json string as above works, however when I validate the string at http://jsonlint.com/ the validation fails. When I pass in a validated json string like this:
{
“json”: {
“BusinessPartner”: “00000000-0000-0000-0000-000000000000”,
“CaseNumber”: 4,
“CaseDescription”: “first case”,
“CaseType”: “”,
“CaseSeverity”: “”,
“DueDate”: “0001-01-01T00:00:00”,
“AssignmentNumber”: 5,
“AssignmentDescription”: “”,
“AssignmentCreation”: “0001-01-01T00:00:00”,
“AssignmentTime”: “0001-01-01T00:00:00”,
“ChangeDate”: “0001-01-01T00:00:00”,
“ChangeTime”: “0001-01-01T00:00:00”,
“Group”: “”,
“SubGroup”: “”,
“Module”: “”,
“AssignmentStatus”: “”,
“KPIChallenge”: false,
“KPI1Status”: “”,
“KPI1User”: “”,
“KPI1Date”: “0001-01-01T00:00:00”,
“KPI1Time”: “0001-01-01T00:00:00”,
“KPI2Status”: “”,
“KPI2User”: “”,
“KPI2Date”: “0001-01-01T00:00:00”,
“KPI2Time”: “0001-01-01T00:00:00”,
“SessionID”: null
}
}
the webservice returns a 500 Internal Server Error, which I believe is because it does not like the argument passed in. I am relatively new to this and I am wondering if anyone can help me understand this a bit better.
Also looking at the response (for the working example), the return json contains three backslaches \\ I believe to escape the quotation marks. This makes this call
JObject jObject = JObject.Parse(jsonData);
JToken jHeatItem = jObject["HeatItem"];
to fail. Any ideas why?
Thanks
Thomas
Feeling like an idiot wasting a lot of time on this, now as everyhing is so easy. Let asp.net take care of it. Followed the steps in this post and all is working perfectly
http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/