REST

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

REST

#1

Postby Steve James » Mon Mar 20, 2017 12:21 pm

Hi, I am writing a REST call to accept a POST from a supplier.

At this stage I am just trying to see what is passed to us using the following code.

Code: Select all

importPackage(com.ebasetech.xi.api);
importPackage(com.ebasetech.xi.services);

try
{
	var endPoint = form.rest.endpointPath;

	var headers = form.rest.getRequestHeaders();
	var pp = form.rest.getPathParameters();
	var rp = form.rest.getRequestParameters();

	log("endPoint " + endPoint);
	log("Headers " + headers);
	log("PathParameters " + pp);
	log("RequestParameters " + rp);

	form.rest.setResponseContentType("text/html");
	form.rest.setResponseBody(endPoint);
}
catch(e)
{
	form.rest.setResponseContentType("text/html");
	form.rest.setResponseBody(e);
}
The logs show the debug of the call plus the log lines in my code. I'm not getting the "Payload" in my log lines. How can I get to the "Payload"? I've added a line in my code to get the body (string) but that doesn't feel right.

Code: Select all

Mon Mar 20 10:45:59: DEBUG Outbound REST:
----------------------------------------------------
Status: 200
Headers: 
Payload: 
/update
----------------------------------------------------

Mon Mar 20 11:01:00: DEBUG Inbound REST:
----------------------------------------------------
POST /floodNetwork/update
Headers: {content-length=[68], host=[datahub.bradford.gov.uk], connection=[keep-alive]}

Parameters: 
Payload: 
{"timestamp":"2017-03-20T11:01:01.260Z","value":0.29027327999999963}
----------------------------------------------------

Mon Mar 20 11:01:00: INFO ** START EXECUTION OF RESTFUL WEB SERVICE floodNetwork using endpoint: update **
Mon Mar 20 11:01:00: INFO Running Endpoint event for update
Mon Mar 20 11:01:00: INFO Executing Javascript script floodNetworkUpdate
Mon Mar 20 11:01:00: INFO endPoint /update
Mon Mar 20 11:01:00: INFO PathParameters {}
Mon Mar 20 11:01:00: INFO Headers {content-length=[68], host=[datahub.bradford.gov.uk], connection=[keep-alive]}
Mon Mar 20 11:01:00: INFO RequestParameters {}
Mon Mar 20 11:01:00: INFO ** END EXECUTION OF RESTFUL WEB SERVICE floodNetwork using endpoint: update **
Mon Mar 20 11:01:00: DEBUG Outbound REST:
----------------------------------------------------
Status: 200
Headers: 
Payload: 
/update
----------------------------------------------------
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Mon Mar 20, 2017 12:39 pm

form.rest.getRequestBody() will give you the body object.

Incidentally, we have found that in general the easiest approach to error handling is to not catch and handle specific errors. If you take out the try/catch logic you should find that in the event of an error:
  • o the response code is set to 500 (server error)
    o the failure mesage is returned as the body
Of course you may not want an Ebase failure message returned to your caller. So you might use something like:

Code: Select all

try
{
  ..
}
catch (e)
{
  log(e)  
  throw("Whoops something has gone wrong");
}
Last edited by Jon on Mon Mar 20, 2017 2:58 pm, edited 1 time in total.
0 x

Steve
Moderator
Moderator
Posts: 414
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#3

Postby Steve » Mon Mar 20, 2017 1:37 pm

Hi Steve,

I am looking at your code and the debug. You are setting the response to the endpoint path. And this is displaying "/update" as the PayLoad for the response. This is correct.

If you want to see the incoming Payload, you need to call:

Code: Select all

var payload = form.rest.getRequestBody();

log("Payload: " + payload); 
Jon mentioned some good points regarding error handling.

If you catch an error and you want to set your own response code, you can also set the response code:

Code: Select all

try 
{ 
  ....
 //findDocument(id) throws error if no document found, so catch it.
  var document = findDoc(id);
  .. 
} 
catch (e) 
{
  log(e)  
  form.rest.setResponseBody("Cannot find document");
  form.rest.setResponseStatus(404);
}
I hope this helps..

Steve
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#4

Postby Steve James » Mon Mar 20, 2017 4:35 pm

Thanks Steve so "payload" = "body" (something new every day...)
And this is displaying "/update" as the PayLoad for the response. This is correct.
Yes at the moment I'm just trying to see that all the data I need is being passed in the request. The supplier isn't sending 1 critical bit of data but can send it in a number of ways.

They are just struggling with caching at their end (not with our endpoint) so nothing is being passed at the moment.

Thanks
0 x


Who is online

Users browsing this forum: No registered users and 125 guests