cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

EMS Rest API call

AnnaAn
13-Aquamarine

EMS Rest API call

Hi,

I'm learning something about EMS http server config from Help center, and by default the http server request port is 8000.

I want to know:

1. does this port(8000 by default) should be the same to one of the the Thingworx server ports that is configured on Tomcat? or they are seperate one?

2. how to request a local rest api call with the port(8000 by default), say from a lua script resource to EMS?

I ask this because after I connected EMS to Thingworx server, I open a rest api call like this:

localhost:8000/Thingworx/Things/MyThing/Properties/myString?method=put&value=fjdk&appKey=xxxxxxxxxxxxx

but it didnt update the property, but it updated the property when I use 8080 instead, and 8080 is what Thingworx is using.

when I access localhost:8000/, it will show the error message as:

  �  HTTP/1.0 400 Bad request

Date: 2015-10-19T10:05:52Z

Server: ThingWorx HTTP Server (Win32)

Connection: close

Content-Type: text/html

Content-Length: 376

<html><head>

<title>400 Bad request</title>

<style type=text/css>

<!--

body{font-family:Verdana,sans-serif;font-size:9pt}

h3{font-size:13pt}

pre{font-family:Courier New,cour}

-->

</style>

</head><body>

<h3><br>400 Bad request</h3>

<p>Your browser sent a request that this server could not understand.</p>

<p><hr noshade size=1>ThingWorx HTTP Server (Win32)</p>

</body></html>

so I guess I should use the rest api through port 8000 in a wrong way. but I dont know how to rest api call EMS in lua or other process.

below is what I configured in config.json for http server:

"http_server":  {

  "host": "localhost",

  "port": 8000,

  "ssl": true,

  "content_read_timeout": 20000

  }

Many thanks,

Br,

Anna

6 REPLIES 6
ckulak
1-Newbie
(To:AnnaAn)

Hello Anna,

First of all, 8000 for EMS and 8080 for TWX are different ports, they don't have to (and on the same machine they can't be) the same.

Here are some ways to troubleshoot this situation:

1. Make sure that GET works first, try to access this URL: localhost:8000/Thingworx/Things/MyThing/Properties/myString

It should return you something like this: {"rows":[{"myString":""}],"fieldDefinitions":{"myString":{"name":"myString","description":"","type":"STRING","aspects":{}}}}

2. Check wsems logs, which it outputs to STDOUT;

3. Overriding HTTP method with "method=put" is discouraged and I believe it's even disabled by default. Instead you should use correct HTTP method, i.e. PUT. There are many ways to test it, the easiest probably is to install a plugin for your browser. I use RESTClient for Firefox (RESTClient, a debugger for RESTful web services. :: Add-ons for Firefox). Here's how I test EMS for my GeoFenceThing thing and "test" property (it sets the value to "abc" as expected):

restclient.png

My EMS config for this test is trivial:

{
    "ws_servers": [{
        "host": "localhost",
        "port": 8080
    }],
    "ws_connection": {
        "verbose": true,
        "encryption": "none"
    },
    "appKey": "xxxxxxx-yyyyyy-zzzzzz"
}

Weirdly it works for me only with EMS 5.3.0.539 (which is currently the latest), but not with 5.2.2.32, so make sure you use the latest version.

Regards,

Constantine

AnnaAn
13-Aquamarine
(To:ckulak)

Dear Constantine,

Many thanks for your reply.

As you mentioned, this issue is fixed after I upgraded to 5.3.0.539(I didnt test against 5.2.2.32 if it's working fine after installed firefox poster) and installed a firefox plugin - firefox poster for  http request test, it's similar tool to what you are using.

in poster I could do PUT/POST/DELETE, but in firefox browser, it will not read anything after the "?",so it only do GET by default. e.g., if I input in browser as: localhost:8000/Thingworx/Things/MyThing/Properties/myString?method=PUT&value=strVal, EMS http server will only parse the part before "?" and will report something like Null string passed iwhich should mean that http server didnt get the request method and the input parameter value, etc. This could only be done successfully in a firefox http request test plugint tool.

thanks for the suggestion that my issue is resolved.

Have a good day.

Br,

Anna

ckulak
1-Newbie
(To:AnnaAn)

Hello Anna,

Indeed, the browser address bar is a rather limited UI for sending HTTP requests. However, it is limited to GETs on purpose, so that you don't modify data by mistake, retry it, etc. In other words, the hyperlink on a web page is not supposed to modify any data, so to say.

There are many alternative ways of sending a PUT or POST request:

  • To debug: browser plugins or advanced tools like SoapUI
  • From command line: typically using curl
  • From your existing application -- depends on many factors. For example, for Java it's supported using OOTB HttpURLConnection​, or you can use a standard JAX-RS​ API, or not-so-standard Apache HttpClient, just to name a few. In case of ThingWorx it's more convenient to use the EDGE SDK, which encapsulates all web service invocations.
  • From web page: using JavaScript, as soon as they are in the same domain with the web service

If you are limited to sending GET requests only, it should be possible to find an alternative solution.

/ Constantine

AnnaAn
13-Aquamarine
(To:ckulak)

Hi Constantine,

Cool.

Thanks a lot for the explanation.

The issue is currently resolved, but I'm sure I will take reference to what you mentioned in future.

Thanks again,

Br,

Anna

Hi,

To test GET/PUT/POST from the browser you can use too Postman extension.

Best Regards,

Carles.

AnnaAn
13-Aquamarine
(To:CarlesColl)

Hi Coll,

Thanks for the information. I will try to use it.

Thanks,

Br,

Anna

Top Tags