HTTP Message format from %Net.HttpRequest
Hello Community,
Is there any built in methods available to generate the both HTTP message format(request/response) from %Net.HttpRequest and it's response.
Thanks!
Comments
I do not understand your question, can you please explain what you need to achieve?
%Net.HttpRequest class is used to create an HTTP request that is sent to the counterpart/endpoint and a response message is then received from the counterpart. How can you possibly generate a response that, by definition, has to be received from a remote system?
Clearly I'm missing something.
Hello @Enrico Parisi
I'm expect/built in method to generate the below HTTP request and HTTP response from %Net.HttpRequest. which was shown in the "View HTTP Trace" option in web gateway.
POST /aktest/test HTTP/1.1
Content-Type: application/fhir+json
Accept: */*
Host: localhost:52773
Accept-Encoding: gzip, deflate, br
Content-Length: 21
{ "asd":"asd" }This is the response from Ask Developer Community AI
Code adapted with your details:
Set objHttpRequest = ##class(%Net.HttpRequest).%New()
Set objHttpRequest.ContentType = "application/fhir+json"Set objHttpRequest.Server = "localhost"Set objHttpRequest.Port = "52773"Set pRequest = {"asd":"asd"}
Do objHttpRequest.EntityBody.Write(pRequest.%ToJSON())
If objHttpRequest.Send("POST", "/aktest/test") {
Set objHttpResponse = objHttpRequest.HttpResponse
If$IsObject(objHttpResponse.Data) {
Set objStream = objHttpResponse.Data
Set json = ""While ('objStream.AtEnd) {
Set json = json _ objStream.ReadLine()
}
} Else {
Set json = objHttpResponse.Data
}
Set httpStatus = objHttpResponse.StatusCode
Write"Status: ", httpStatus, !
Write"Response: ", json, !
}Thanks for the code samples. my expectation is to convert the %Net.HttpRequest to Http request standard message structure. Actually Mr @Jeffrey Drumm gives me the expected solution. The Http Get , Post methods has the second parameter "test" it helps me to get that standard http message string
Are you looking to view the expected request and/or response? The %Net.HttpRequest Send() method has a test argument as its 3rd option; setting it to 1 outputs the request, 2 the response, and 3 the response headers.
It's unfortunate that this isn't described in the class documentation; you have to look at the source to figure out what the test argument does.
Thanks for pointing the solution. By using this integer we can get the request if test is 1or response test=2 or test = 3 headers. However, As of my understanding we didn't get both request and response at the same time.
1 outputs but does not send the request, so just call Send with 1 first, then with 2 to get both request and response.
💡 This question is considered a Key Question. More details here.