Skip to main content
1-Visitor
January 29, 2018
Question

Invalid API Key when making REST call

  • January 29, 2018
  • 8 replies
  • 7124 views

My REST API call to query my datatable keeps returning the following error in the logs: 

Error [authenticating connection], Server websocket will be closed. cause: Invalid Application Key c.t.c.s.c.j.Jsr356ServerConnection http-nio-8181-exec-86

This is my REST call:

http://<myHost>/Thingworx/Things/<myDataTable>/Services/QueryDataTableEntries?method=post&userid=<User>r&password=<Password>&appKey=<my Api Key>&x-thingworx-session=true&content type=application/json&Accept=application/json&maxItems=10&query=<encoded json param>

I double checked that I indeed copied the api key correctly and that the key is associated with the user I am passing in.

When I pass in a simple json query I get the following warning in my logs instead but all I get results back.

Log:

Server WebSocket closed unexpectedly and has already been unregistered from Server Endpoint [ws session id: 5a1e7] An established connection was aborted by the software in your host machine

Json Query:

{"filters":{"type": "And","filters":[{"type": "OR","filters":[{"type": "LIKE","fieldName": "ASSETCODE","value":"Pump"},{"type": "LIKE","fieldName": "ASSETDESC","value": "Pump"},{"type": "LIKE","fieldName": "ASSETCLASS","value": "Pump"},{"type": "LIKE","fieldName": "CLASSDESC","value": "Pump"},{"type": "LIKE","fieldName": "MANUFCODE","value": "Pump"},{"type": "LIKE","fieldName": "MFGDESC","value": "Pump"},{"type": "LIKE","fieldName": "MFGMODEL","value": "Pump"},{"type": "LIKE","fieldName": "ASSETCATE","value":"Pump"},{"type": "LIKE","fieldName": "CATEDESC","value": "Pump"},{"type": "LIKE","fieldName": "ORG","value": "Pump"}]}]}}

But when I do a compound query like this:

{"filters":{"type": "And","filters":[{"type": "OR","filters":[{"type": "LIKE","fieldName": "MFGMODEL","value":"Pump"},{"type": "LIKE","fieldName": "ASSETCODE","value": "Pump"},{"type": "LIKE","fieldName": "ASSETDESC","value": "Pump"},{"type": "LIKE","fieldName": "ORG","value": "Pump"}]},{"type": "OR","filters":[{"type": "IN","fieldName": "ORG","values":["ALB","KAL"]}]}]}}

I get the invalid api key log mentioned above and my connection times out without returning anything.

Any advice is much appreciated.

8 replies

22-Sapphire I
January 30, 2018

First of all, in this case, don't use Session = True because you don't need a session and you will be spinning of sessions needlessly.

Second, check PlatformSubsystem configuration to see if it allows the appkey in URL or not.

Thingworx now by default blocks this, because it is recommended that the appkey is passed in the header not on the url for security reasons.

kbetances1-VisitorAuthor
1-Visitor
January 30, 2018

Thanks! I checked all this and all seems okay. It must be something with my query. Something like this isn't working for me but I cant figure out why.

{

     "filters":

     {

          "type": "And",

          "filters":

          [

               {

                    "type": "OR",

                    "filters":

                    [

                         {

                              "type": "LIKE",

                              "fieldName": "MFGMODEL",

                              "value":"Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ASSETCODE",

                              "value": "Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ASSETDESC",

                              "value": "Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ORG",

                              "value": "Pump"

                         }

               ]

         },

          {

               "type": "OR",

               "filters":

               [

                    {

                         "type": "IN",

                         "fieldName": "ORG",

                         "values":["NAG","KAL"]

                    }

               ]

          }

   ]

}

}

5-Regular Member
January 30, 2018

Kelvin Betances: Could you please confirm the ThingWorx version you are using. Please follow below steps and see it it help resolve the issue:

1. Enable or Tick the configuration "Allow Request Method Switch" going to Subsystem -> Platform Subsystem . Please note this setting expose details to external system so needs to be review correctly from security perspective.

2. Make sure the expiry of the app key is future date and is valid.

-Durgesh

kbetances1-VisitorAuthor
1-Visitor
January 30, 2018

I have Thingworx version 8.

I checked both things and it seems to be fine. It has to be my query but I cant find the syntax error:

{

     "filters":

     {

          "type": "And",

          "filters":

          [

               {

                    "type": "OR",

                    "filters":

                    [

                         {

                              "type": "LIKE",

                              "fieldName": "MFGMODEL",

                              "value":"Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ASSETCODE",

                              "value": "Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ASSETDESC",

                              "value": "Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ORG",

                              "value": "Pump"

                         }

               ]

         },

          {

               "type": "OR",

               "filters":

               [

                    {

                         "type": "IN",

                         "fieldName": "ORG",

                         "values":["NAG","KAL"]

                    }

               ]

          }

   ]

}

}

5-Regular Member
January 30, 2018

Kelvin Betances in your query the first filter has a type 'And' which should be "AND", rest looks good. Once you make that update run this within Composer and see if you do have a data that matches this filter! Once you know the response withing composer then try running a REST service externally and see if you still see error messages in the log along with expected result in the output

5-Regular Member
January 30, 2018

You are using Post, sometimes I had to, in the app key, enable to the user execute services.