User bio
404 bio not found
Rome, Italy
Member since Jul 24, 2023
Pinned posts:
Replies:

Hi, it's been quite a while since this question but yesterday I had the same problem and I'd like to share how I managed to solve it.

First of all, I have created a simple unauthenticated Web Application where the dispatch class it the EnsLib.REST.GenericService in order to user the simpler and cleaner architecture.

Then, after processing the HTTP request, I generate the HTTP response through a Business Process where I have declared the following parameters:

Parameter CONTENTTYPEJSON As STRING = "application/json";
Parameter HTTP200OK As STRING = "HTTP/1.1 200 OK";
Parameter HTTP202ACCEPTED As STRING = "HTTP/1.1 202 Accepted";
Parameter HTTP400BADREQUEST As STRING = "HTTP/1.1 400 Bad Request";
Parameter HTTP500INTERNALSERVERERROR As STRING = "HTTP/1.1 500 Internal Server Error";

It is important that the parameters related to the status line starts with "HTTP/1.1", since it is this statement that enable the return of the correct HTTP status.

Finally, I generate the response as shown:

Set ContentType = ..#CONTENTTYPEJSON
Set StatusLine = ..#HTTP500INTERNALSERVERERROR
Set HTTPStatus = 500

; Format the json with the correct indentation
Set jsonFormatter = ##class(%JSON.Formatter).%New()
Set pStream = ##class(%Stream.GlobalCharacter).%New()
Set sc = jsonFormatter.FormatToStream(json, .pStream)

; Generate an HTTP response message
Set pResponse = ##class(EnsLib.HTTP.GenericMessage).%New(pStream)

; Add headers to the HTTP response
Set sc = pResponse.HTTPHeaders.SetAt(StatusLine,"StatusLine")
Set sc = pResponse.HTTPHeaders.SetAt(HTTPStatus,"StatusCode")
Set sc = pResponse.HTTPHeaders.SetAt(ContentType, "Content-Type")

; I tested it with the following HTTP codes either and it works: 
;   202 / ..#HTTP202ACCEPTED
;   200 / ..#HTTP200OK
;   etc...

I tested with HTTP statuses 200, 202 and 500, but I'm pretty sure it will work with other statuses as well.

Hello Ashok, this is a great article, thanks for sharing it!

However, I would like to ask how to create an oref using a property index. For example, considering the class:

Class User.Test Extends %Persistent
{

Property Code As %String(MAXLEN = 50) [ Required ];
Index CodeIdx On CodeIdx [ Unique ];
}

In ObjectScript I can use the following syntax to open the oref using its ID: 

set obj = ##class(User.Test).CodeIdxOpen(ID)

In the same way I can use the CodeIdxDelete or CodeIdxExists as well.

I was wondering how to obtain the same result in Embedded Python.

Certifications & Credly badges:
Global Masters badges:
Followers:
Following: