19 Replies Latest reply on Dec 1, 2016 3:10 PM by pthivierge

    PI Web API POST Problem (Cors)

    JensPr

      Hi there,

      though I have Pi Web API up and running for GET, it refuses with POST. And I do not get it.

      It is not complaining for CORS when doing GET.  Anyone a clue?

       

      Here is the configuration:

      {
        "AuthenticationMethods": [
        "Anonymous"
        ],
        "CorsExposedHeaders": "Allow,Content-Encoding,Content-Length,Date,Location",
        "CorsHeaders": "accept, authorization, content-type",
        "CorsMethods": "GET,OPTIONS,POST",
        "CorsOrigins": " http://swhars6004.swde2.local, http://plog-sto-01.int.nynas.com",
        "CorsSupportsCredentials": true,
        "DisableWrites": true,
        "SearchBoosts": [
        1.0,
        0.8,
        0.5,
        0.5,
        0.5,
        0.5,
        0.5
        ],
        "SearchPointAttributes": [
        "pointsource",
        "instrumenttag",
        "location1",
        "exdesc"
        ],
        "SearchScanInterval": 180
      }
      

       

      Here is the Code:

      function postTest() {
      
        var req = {
         "1": {
           "Method": "GET",
           "Resource": "https://piafhar-sto-01/piwebapi/streamsets/interpolated?webID=P03rCkp0judEazHPxrmYHHRQRx0AAAUElIQVItU1RPLTAxXFRLU1QwMjAxLkdST1NTUVRZ&webID=P03rCkp0judEazHPxrmYHHRQSR0AAAUElIQVItU1RPLTAxXFRLU1QwMjAxLkZSRUVRVFk&webID=P03rCkp0judEazHPxrmYHHRQUx0AAAUElIQVItU1RPLTAxXFRLU1QwMjAxLkxFVkVM&startTime=10/01/2016%2000:00&endTime=10/03/2016%2000:00&interval=15m",
           "Content": "{\"Name\":\"New Element\"}",
           "Headers": {
             "Cache-Control": "no-cache"
           }
         },
         "2": {
           "Method": "GET",
           "Resource": "https://piafhar-sto-01/piwebapi/streamsets/interpolated?webID=P03rCkp0judEazHPxrmYHHRQ-RgAAAUElIQVItU1RPLTAxXFRLTlQwNTIxLkdST1NTUVRZ&webID=P03rCkp0judEazHPxrmYHHRQ-xgAAAUElIQVItU1RPLTAxXFRLTlQwNTIxLkZSRUVRVFk&webID=P03rCkp0judEazHPxrmYHHRQBRkAAAUElIQVItU1RPLTAxXFRLTlQwNTIxLkxFVkVM&startTime=10/01/2016%2000:00&endTime=10/03/2016%2000:00&interval=15m",
           "Content": "{\"Name\":\"New Element\"}",
           "Headers": {
             "Cache-Control": "no-cache"
           }
         }
        };
      
        $.ajax({
           type: "POST",
           url: "https://piafhar-sto-01/piwebapi/batch",
           dataType: 'json',
           async: true,
           data: req,
           success: function(piData) {
            console.log(piData)
        },
           cache: true,
        error: function (xhr) {
        console.log(xhr.responseText);
        },
          beforeSend: function (xhr) {
        xhr.setRequestHeader('Authorization', 'Anonymous');
          }
        });
      
      }
      
      
      
      
      
      dd
      
        • Re: PI Web API POST Problem (Cors)
          pmartin

          Hi Jens,

           

          What error are you getting? Unauthorized?

          Also, it might be easier to test with a simpler method than batch.  Something like streams UpdateValue request would make it easier to verify that it is a network related issue.

          • Re: PI Web API POST Problem (Cors)
            pthivierge

            Hello Jens,

             

            Have you looked at the configuration for your PI Web API installation under the Configuration AF Database?

             

            Here is the configuration I have on my dev box.  You should NOT use CorsOrigins=* on a production installation!

             

             

            Changes are normally picked up quite fast but to make the test right after you may restart the PI Web API after the change.

             

            ps: Also make sure you have a look at the PI Web API User Guide:  https://techsupport.osisoft.com/Products/Developer-Technologies/PI-Web-API/

             

            Hope this helps,

            2 of 2 people found this helpful
            • Re: PI Web API POST Problem (Cors)
              pthivierge

              Jens, I just realized you posted your configuration already, I am looking into it...

              • Re: PI Web API POST Problem (Cors)
                pthivierge

                Hello Jens,

                 

                I have tested your code on my end and to get it to work I had to make few small changes:

                • The Content-Type header needs to be set to application/json, with JQuery, this is done with the contentType setting.
                • before passing the JSON object, I use the JSON.Stringify method, otherwise I was getting an bad-request error.

                 

                My AuthenticationMethods is set to Anonymous as yours.

                 

                function postTest() {
                
                
                    var sinusoidWebID = "P0ehg8Y-5HT0y_SFyRRogjcwWgAAAAUElERU1PMjAxNlxTSU46SE9VUg";
                    var sinusoiduWebID = "P0ehg8Y-5HT0y_SFyRRogjcwXAAAAAUElERU1PMjAxNlxTSU46SDI";
                
                
                    var body = {
                        "1": {
                            "Method": "GET",
                            "Resource": "https://192.168.1.1/piwebapi/streamsets/interpolated?webID=" + sinusoidWebID + "&startTime=2016-10-01&endTime=2016-10-03&interval=15m"
                        },
                        "2": {
                            "Method": "GET",
                            "Resource": "https://192.168.1.1/piwebapi/streamsets/interpolated?webID=" + sinusoiduWebID + "&startTime=2016-10-01&endTime=2016-10-03&interval=15m"
                        }
                    };
                
                
                
                
                    $.ajax({
                        type: "POST",
                        url: "https://192.168.1.1/piwebapi/batch",
                        contentType:'application/json',
                        async: true,
                        data: JSON.stringify(body),
                        success: function (piData) {
                            console.log(piData);
                        },
                        cache: false,
                        error: function (xhr) {
                            console.log(xhr.responseText);
                        },
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader('Authorization', 'Anonymous');
                        }
                    });
                }
                

                 

                Please let me know how it goes!

                  • Re: PI Web API POST Problem (Cors)
                    JensPr

                    Thanks for helping out. Patrice, I used your modified code.. But it still does not work:

                     

                    function postTest() {
                    
                      WebId1 = "P03rCkp0judEazHPxrmYHHRQNB0AAAUElIQVItU1RPLTAxXFRLU1QwMjAwLkdST1NTUVRZ";
                      WebId2 = "P03rCkp0judEazHPxrmYHHRQNh0AAAUElIQVItU1RPLTAxXFRLU1QwMjAwLkZSRUVRVFk";
                       
                      var body = {
                       "1": {
                         "Method": "GET",
                         "Resource": "https://piafhar-sto-01/piwebapi/streamsets/interpolated?webID=" + WebId1 + "&startTime=2016-10-01&endTime=2016-10-03&interval=15m"    
                       },
                       "2": {
                         "Method": "GET",
                         "Resource": "https://piafhar-sto-01/piwebapi/streamsets/interpolated?webID=" + WebId2 + "&startTime=2016-10-01&endTime=2016-10-03&interval=15m"
                       }
                      };
                    
                      $.ajax({
                         type: "POST",
                         url: "https://piafhar-sto-01/piwebapi/batch",
                         dataType: 'json',
                         contentType:'application/json',
                         async: true,
                         data: JSON.stringify(body),
                         success: function(piData) {
                          console.log(piData)
                      },
                         cache: false,
                      error: function (xhr) {
                      console.log(xhr.responseText);
                      },
                        beforeSend: function (xhr) {
                      xhr.setRequestHeader('Authorization', 'Anonymous');
                        }
                      });
                    
                    }
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    

                     

                    Results in:

                    prob.PNG

                  • Re: PI Web API POST Problem (Cors)
                    JensPr

                    Ok will check that.

                    What strikes me: GET is working, but POST does not return the Access-Control-Headers.
                    See screenshot.

                    111Capture.PNG