15 Replies Latest reply on Feb 7, 2018 5:07 AM by csameer

    HTTP Headers in Alert Actions - POST/Get URL

    jmannbaylor

      I'm attempting to write an alert action that POSTs data to a URL (Cisco Spark Bot message).  Cisco requires you include HTTP Headers with your authentication token.  Without having to resort to run a powershell script, is there any way with the Native POST action to include additional HTTP headers?

       

      Updating most  modern REST services would require this (the auth token is generally something you include in the header).

        • Re: HTTP Headers in Alert Actions - POST/Get URL
          Leon Adato

          Let me dig into this. Also flagging jbiggley Dez KMSigma patrick.hubbard & cobrien because they KNOW THINGS.

            • Re: HTTP Headers in Alert Actions - POST/Get URL
              jbiggley

              I've never tried this and it is on the periphery of my skills, but if you stored the auth token as a custom property you should be able to use the POST method and simple add the variable for the custom property into the URL.  That feels like a bit of a hack way to do it though.

               

              You might be better off doing it via PowerShell.

            • Re: HTTP Headers in Alert Actions - POST/Get URL
              Patrick Hubbard

              I can see a couple of other reasons you'd want to manipulate headers as well, like a REST API that requires a particular content-type or otherwise it barfs, spews XML when you least desire it or other general missfeatureism. Proxy directives, caching control, user-agent and other headers are also candidates for specific settings.  I've been nagging the platform team for a REST/POST action for a couple of years, and will renew the push.  You can also add it to the feature request list in THWACK and I'll up-vote it.

               

              In the meantime, Powershell is the way to go, especially because you could standardize many of the elements you need and pass way fewer elements, way easier, than a wonky URI shoehorned into an alert field.  For example, you'd have one script with your authentication, access tokes and everything else, and could even restrict access to that file for security so that only admins and Orion could see/execute it.  That way you're not putting authentication details in every alert that's calling the script.

               

              Hope that helps.

              1 of 1 people found this helpful
              • Re: HTTP Headers in Alert Actions - POST/Get URL
                KMSigma

                I'm not an expert on the Cisco Spark interface, but I did just test it out with a little bit of PowerShell goodness.  So, the simple answer is that this is possible, and not too difficult.

                 

                # Script Variables
                $User        = "this is your bot's email @ sparkbot.io"
                $AccessToken = "put the bot's access token here"
                
                # Create a Dictionary Object for the Header information
                $Headers = @{ Authorization = "Bearer $AccessToken" }
                
                # A few URI's that I got from the Cisco Dev pages - there are more.
                $PeopleUri = "https://api.ciscospark.com/v1/people?email=$User"
                $RoomsUri  = "https://api.ciscospark.com/v1/rooms?email=$User"
                
                # Query for users
                # - Use headers for authentication
                # - Make a request using the "GET" method)
                $Results = Invoke-RestMethod -Uri $PeopleUri -Method Get -Headers $Headers
                $Results.Items
                
                # Create something
                # - Use same headers for authentication
                # - Craft the "body" of the message 
                # - Convert that "body" to Json
                # - Make a request using the "POST" method
                $ContentType = "application/json"
                $RoomContent = @{ title = "Test Room" } | ConvertTo-Json
                $Results = Invoke-RestMethod -Uri $RoomsUri -Headers $Header -ContentType $ContentType -Method Post -Body $RoomContent
                $Results.Items