AnsweredAssumed Answered

C# Post Request 500 - Internal Server Error when calling /Displays/SaveDisplay

Question asked by nicka on Jun 12, 2017
Latest reply on Jun 22, 2017 by nicka

I'm attempting to make a simple C# application which does some things with PI Vision displays (Coresight 2016 R2), however I'm having difficulties successfully making a POST request. The below is a minimal example of what I am attempting and it involves generating a fresh PI Vision display by requesting a new display and adding its request ID to a pre-built framework accepted by PI Vision when saving display:

using System.Net;
using Newtonsoft.Json;

private void saveDisplay() {
  string blankDisplay = JsonConvert.SerializeObject(generateBlankDisplay());
  WebRequest request = WebRequest.Create("https://<servername>/Coresight/Displays/SaveDisplay");
  request.Credentials = CredentialCache.DefaultCredentials;
  request.Method = "POST";
  request.ContentType = "application/json";
  using(var streamWriter = new StreamWriter(request.GetRequestStream())) {
  streamWriter.Write(blankDisplay);
  streamWriter.Flush();
  }
  Stream objStream = request.GetResponse().GetResponseStream();
  StreamReader objReader = new StreamReader(objStream);
  Console.WriteLine(objReader.ReadToEnd());
}

private dynamic generateBlankDisplay() {

  WebRequest request = WebRequest.Create("https://<servername>/Coresight/Displays/NewDisplay");
  request.Credentials = CredentialCache.DefaultCredentials;

  Stream objStream = request.GetResponse().GetResponseStream();
  StreamReader objReader = new StreamReader(objStream);
  dynamic resDisplay = JsonConvert.DeserializeObject(objReader.ReadToEnd());

  dynamic blankDisplay = JsonConvert.DeserializeObject(/*Omitted for brevity*/);

  blankDisplay.Display.RequestId = resDisplay.RequestId;
  blankDisplay.Display.Name = "TestDisplay";
  return blankDisplay;
}

 

After generateBlankDisplay has been called withing saveDisplay, the contents of blankDisplay is:

 

{
    "StartTime": "*-8h",
    "EndTime": "*",
    "EventFramePath": null,
    "Display": {
        "EventFramePath": null,
        "Id": -1,
        "Name": "TestDisplay",
        "RequestId": "e48b9346-2155-49a9-99d0-f1667a2e6cc0",
        "Revision": 1,
        "DisplayProperties": {
            "EventFrameSearch": null
        },
        "Symbols": []
    },
    "Attachments": [],
    "CurrentElement": null,
    "TZ": "Europe/London"
}

 

This doesn't work, however when I make the same request by copying the above JSON (it's actually a flat string, formatted here for viewing) into a webservices.postSaveDisplay request the request is successful:

 

angular.element(document.body).injector().get('webServices').postSaveDisplay({
    "StartTime": "*-8h",
    "EndTime": "*",
    "EventFramePath": null,
    "Display": {
        "EventFramePath": null,
        "Id": -1,
        "Name": "TestDisplay",
        "RequestId": "e48b9346-2155-49a9-99d0-f1667a2e6cc0",
        "Revision": 1,
        "DisplayProperties": {
            "EventFrameSearch": null
        },
        "Symbols": []
    },
    "Attachments": [],
    "CurrentElement": null,
    "TZ": "Europe/London"
})

 

Am I missing some headers in my C# or is something unknown going on?

 

As an additional question, is the request ID necessary or can I forget about it for the most part? What's it there for?

Outcomes