Question
Yone Moreno · Mar 5

Creating a RESTFul Service, why custom method does not return the expected JSON?

FIrst of all thank you for your time in reading this question and writing a response,

We would need some help,

-> Our objective is to control which method is being used in the service: GET POST PUT

We have tried to understand the example REST Service: Demo.REST.DirectoryService

 

After that we have tried to create our own custom rest service,

please take a few minutes to examine the following code:

 
Code

We are able to use "retrievePerson" succesfully

We send:

http://localhost:19622/aplicaciones/scs/test/miscs/employee/name/Q*/salary

We observe:

With headers:

 

Here comes the challenge:

When we issue:

http://localhost:19622/aplicaciones/scs/test/miscs/consultarImagen

With the following body:

{
    "idApp": "miHistoria",
    "usuario": "11473564",
    "numExpediente": "11473564"
}

It replies nothing:

With the headers:

Besides if we check the trace we see:

 

 

Why we do not see the reply in POSTMAN?

Why headers are "text/html" and not "application/json"?

Could you help us?

 

We have read:

https://docs.intersystems.com/latest/csp/documatic/%25CSP.Documatic.cls?...

https://docs.intersystems.com/latest/csp/docbook/Doc.View.cls?KEY=GREST_...

 

Could you point us to some code, examples or documentation?

 

Thank you for your replies

 

00
2 0 12 161
Log in or sign up to continue

If you are sending a body in your request to the REST service then it needs to be a POST, not a GET.

<Route Url="/consultarImagen" Method="GET" Call="consultarImagen"/>

Thanks Marc Mundt for your reply

Yes you are right, we should use POST

We have changed it:

<Route Url="/consultarImagen" Method="POST" Call="consultarImagen"/>

However we do not see the response in POSTMAN:

And the headers are:

Content-Type: text/html

Content-Length: 0

CACHE-CONTROL: no-cache

PRAGAM: no-cache

We send the POST to the following URL:

http://localhost:19622/aplicaciones/scs/test/miscs/consultarImagen
 

Besides we observe the response message being converted from Ensemble object to JSON in the service:

We do see the Response Message from the Operation to the Service

Why we do not see the JSON being replied from the Service in POSTMAN?

How could we debug this behaviour?

Thanks for your replies

Thanks Marc Mundt for your attention, and your helpful reply

 
Our current code is:

When we dig deeper using Whireshark:

First, we find that the method in the example that uses GET, the retrievePerson, does answers with a JSON to POSTMAN, so it works:

GET request:

GET response:

In POSTMAN:

However,

Our custom method: consultarImagen which is a POST

Shows nothing in POSTMAN

Why?

POST request:

POST response:

In POSTMAN:

Could you help us?

Thanks for your replies

Yone, try removing this line and test if it works:

Set pOutput=##class(%GlobalBinaryStream).%New()

Thanks Marc Mundt for your help

You are right

When we removed that line, the response is shown in POSTMAN:

Thanks four your help Marc

How did you know we should remove that line?

I noticed that retrievePerson doesn't instantiate pOutput.

We are grateful Marc for your help,

Thanks for explaining how did you find the cause which was preventing to get the response in POSTMAN

I would recommend using plain %CSP.REST and calling BP/BO from it. Here's how. You'll need to replace async call with a sync one to get response back, but the rest of the logic would be the same.

Thanks Eduard for sharing this interesting piece of advice

Why would be better or recommended to use %CSP.REST directly, instead of EnsLib.REST.Service?

Are there any improvements if we use %CSP.REST?

Thanks for your reply

Why would be better or recommended to use %CSP.REST directly, instead of EnsLib.REST.Service?

To start with using %CSP.REST is the recommended way to use REST with productions.

Are there any improvements if we use %CSP.REST?

It's easier to develop, there are more examples available, you can have a combined production/non-production REST services easily.

Hi Eduard,

I am looking for some examples of REST API served through production. Would you be able to point me to some, please? 

I am trying to implement a business service,  process and operation that serves bundle of apis related to a domain and resource e.g Patient , Patient alerts, patient status . These will be 3 api urls served by single business service,  process and operation.  

I am also looking for best practice to handle various http responses in BP and BO.

Would greatly appreciate if you can direct me to relevant examples. 

Thank you,

Utsavi