Skip to main content
1-Visitor
December 18, 2013
Question

REST API handling post request

  • December 18, 2013
  • 2 replies
  • 4652 views

I have to expose one REST API which will be handling post request in thingsworx.

Requirement :

1.      API which will handle post request and extract request post data in form of json.

2.       API will be called from external system

2 replies

1-Visitor
December 18, 2013

Hi Rohit, if I understand you correctly then all you need to do is to create a Service in ThingWorx that the external system can call.

One of the Input parameters into the service will be either String or JSON so your call can pass that into ThingWorx.

rohitk1-VisitorAuthor
1-Visitor
December 18, 2013

yes correct but it has to be "post" request not "get".



1-Visitor
December 18, 2013

I apologize looks like I put the uri for a GET not a POST.

For a POST you would do:

ServerAddress/Thingworx/Things/NameofThing/Services/NameofService?method=post&postParameter=<name of input parameter that gets the post document>&Accept=application/json

(use Accept=text/xml for xml content)



12-Amethyst
December 21, 2015

I am getting the following error.

Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 500

  at com.thingworx.http.client.HttpConnectionClient.main(HttpConnectionClient.java:76)

Here is the snippet which does my HTTP Connection.

    http_connection.setInstanceFollowRedirects( false );

    http_connection.setRequestMethod( "POST" );

    //http_connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");

    http_connection.setRequestProperty( "Content-Type", "text/xml");

    http_connection.setRequestProperty( "charset", "utf-8");

    http_connection.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));

  

  

    try( DataOutputStream wr = new DataOutputStream( http_connection.getOutputStream())) {

      wr.write( postData );

    }