I just started setting up a framework for testing the API's and I'm using specflow with C# and VS. I'm able to send a request and receive a JSON response, however when I tried to Deserialise the response.Content I am getting an error. I think it is because the response.Content string has forward slashes in them as shown below. I tried removing them before deserializing but it does not remove it as I can see it in the debugging mode.
Error I get is: An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Unexpected character encountered while parsing value: . Path '', line 0, position 0.
respJson - "{"Result":{"isLinked":true,"saleDateTime":"16/06/2017 14:20:20","storeName":"UAT1 BRIGHTON LOC 4"},"Status":{"ActionType":0,"IsSuccess":true,"ActionString":""}}"
Method - public static dynamic receiptRetrieve(string uri, Method method, string inputJsonFileName, Dictionary inputParams = null){
object temp = GetJsonInput(inputJsonFileName, inputParams);
var response = RestFeedPlugin.SendRequestToEReceiptsAPI(uri, method, null, temp);
string respJson = response.Content.Replace(@"\", @"");
dynamic jsonObject = JsonConvert.DeserializeObject(respJson);
return jsonObject;
}
I figured what the issue is. The json responseI'm getting from RestSharp is adding a space at the start which causes it to break. If I remove the first character from the response.content it seems to be working. However, this is not an ideal solution and I'm still open to thoughts on why this is happening at the first place? Temporary solution to my problem -
var response = client.Execute(request);
response.Content = response.Content.Remove(0, 1);
client.BuildUri(request);
Those backslashes are escaping the apostrophes inside the text. They are meant to be there. Your program works fine without removing them:
You can see it in action here: https://dotnetfiddle.net/rJwaJM