Troubleshooting REST Operation
I am trying to replicate a REST call that I am able to make via a Postman call within a EnsLib.REST.GenericOperation.
It's been a while since I have messed around with trying to make external REST calls. When I execute my REST call, tSC is coming back with an error and I am trying to pinpoint why. I tried turning on ISCLOG = 5 but when calling the REST Operation from the Testing tool it is not logging anything to the ISC log.
How do we see the RAW request being sent out to verify that my request is formatted properly? tSC is coming back with an error as the message displayed in the error is "Error in sending request to server"
Method PostSearchPerson(pRequest As COM.REST.Msg.Request.PostSearchPerson.PostSearchPersonRequest, Output pResponse As COM.REST.Msg.Response.PostSearchPerson.PostSearchPersonResponse) As%Status
{
#dim tSC As%Status = $$$OK
set tHTTPRequest = ##class(%Net.HttpRequest).%New()
set tHTTPRequest.SSLConfiguration = ..Adapter.SSLConfig
set tHTTPRequest.Https = 1
set tHTTPRequest.WriteRawMode = 1
set tHTTPRequest.Port = ..Adapter.HTTPPort
do tHTTPRequest.SetHeader("Host", ..Adapter.HTTPServer)
Do tHTTPRequest.SetHeader("Accept-Encoding","application/json")
Do tHTTPRequest.SetHeader("Content-Type","application/json")
Do tHTTPRequest.SetHeader("api-key",..ApiKey)
do tHTTPRequest.EntityBody.Write()
do tHTTPRequest.OutputHeaders()
set tRequest = ##class(%DynamicObject).%New()
set tRequest.searchString = pRequest.searchString
set tPayload = tRequest.%ToJSON()
set tURL=..Adapter.URL_"/persons/search"
set tSC = tHTTPRequest.EntityBody.Write(tPayload)
set tHTTPResponse = ##class(%Net.HttpResponse).%New()
set tSC = ..Adapter.SendFormDataArray(.tHTTPResponse, "POST", tHTTPRequest, tURL, tPayload)
if$$$ISERR(tSC) {
set tSC = $$$ERROR($$$GeneralError, "Error in sending request to the server")
quit tSC
}
set responseData = {}.%FromJSON(tHTTPResponse.Data)
set pResponse = ##class(COM.REST.Msg.Response.PostSearchPerson.PostSearchPersonResponse).%New()
set tSC = pResponse.%JSONImport(responseData)
quit tSC
}Thanks
Comments
Hello @Scott Roth
You can see the http request format by using "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." Can you check this from the post.
You did not provide details, so I try to guess.
You are writing a Business Operation that use the EnsLib.HTTP.OutboundAdapter, if so, double check the signature of the method SendFormDataArray() that is:
Method SendFormDataArray(Output pHttpResponse As %Net.HttpResponse, pOp As %String, pHttpRequestIn As %Net.HttpRequest, pFormVarNames As %String = "", ByRef pData, pURL As %String) As %Status
Your call is:
set tSC = ..Adapter.SendFormDataArray(.tHTTPResponse, "POST", tHTTPRequest, tURL, tPayload)
I think the passed arguments don't match the method signature.
Here's where you're hurting yourself:
if$$$ISERR(tSC) {
set tSC = $$$ERROR($$$GeneralError, "Error in sending request to the server")
quit tSC
}tSC is already a status. You are changing it to a different status and forcing it to say "Error in sending request to the server." Replace that whole block of code with:
quit:$$$ISERR(tSC) tSCThat will quit if tSC is an error and give you the actual status that occurred. Once you have that, it'll hopefully be easier to troubleshoot this.