0

Hey I am having a weird issue I can't figure out. Basically I am trying to make a simple httpClient post request but I keep getting an "Invalid json primitive:." I have checked on https://jsonlint.com/ and it says this is valid json but my application states otherwise. Any info would be greatly appreciated?

string test = "{\"CurrentUser\":null,\"Stacktrace\":\"System.UnhandledExceptionEventArgs\",\"OtherData\":null,\"UserInput\":\"\",\"TimeStamp\":\"2017-10-10T16:48:58.606512-04:00\"}"

 HttpContent httpContent = new StringContent(test);
 await httpClient.PostAsync("/api/logException", httpContent);

And the client is initialized like:

 httpClient = new HttpClient();
 httpClient.BaseAddress = new Uri(this.serverURL);
 httpClient.Timeout = TimeSpan.FromSeconds(400);
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
Seamy
  • 287
  • 4
  • 14
  • Could you please show how you initialize your HttpClient? – Tomas Smagurauskas Oct 10 '17 at 21:17
  • Done but I don't think there's anything special going on here. I can actually do other post requests but this specific one seems to be the issue. If it helps this is for logging unhandled exceptions to a database – Seamy Oct 10 '17 at 21:20
  • Just built and ran this, and everything seems to be working fine. I think the problem should be somewhere deeper in your configuration. – Tomas Smagurauskas Oct 10 '17 at 21:23
  • Can you look into the `/api/logException` endpoint? Also, it will be helpful if you can post full exception details, like the error message and stack trace. – TheVillageIdiot Oct 10 '17 at 21:50

4 Answers4

2

Instead of writing the JSON format on your own (which is prone to error), how about you simply create an object and serialize it to JSON. Have c# do the heavy lifting. First create a class like:

public class Model
{
   public string CurrentUser { get; set; } = null;
   public string Stacktrace { get; set; } = "System.UnhandledExceptionEventArgs";
   public string UserInput { get; set; } = String.Empty;
   public string OtherData { get; set; } = null;
   public string TimeStamp { get; set; } = "2017-10-10T16:48:58.606512-04:00";
}

Then initialize an object of this class and simply use JavaScriptSerializer to serialize it into json:

Model md = new Model();
System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
var json = ser.Serialize(md);

The result will be in json:

{"CurrentUser":null,"Stacktrace":"System.UnhandledExceptionEventArgs","UserInput":"","OtherData":null,"TimeStamp":"2017-10-10T16:48:58.606512-04:00"}
Sparsha Bhattarai
  • 693
  • 11
  • 20
0

I'm no expert, but since nobody else has responded, perhaps you just need to set the content type to JSON?

HttpContent httpContent = new StringContent(test, Encoding.UTF8, "application/json");

See POSTing JsonObject With HttpClient From Web API

Kudu2
  • 56
  • 2
  • 7
0

Try to String.Replace("\", string.Empty) to your json. Perhaps this will work.

0

Hey thanks for the answers. You are all correct and I appreciate the help. The issue was in the end not actually json. Basically this error was propagating and actually appearing twice after I thinking it was handled. So where I thought it was telling me invalid json existed (where it was breaking) was actually it repeating the same error message from further up the code.

Thanks again.

Seamy
  • 287
  • 4
  • 14