Post request through HTTPOuboundAdapter send empty body ( JSON format )
Hello.
I'm trying to POST a JSON on an URL.
But the body request arrive empty on the web service :
[DEBUG] # 2022-05-05 15:04:32,966 # scopes founds : api
[DEBUG] # 2022-05-05 15:04:32,971 # PatientController:PostSignaletic object null
Here is the code, i tried a TONS of ways to do it but i'm still in trouble ...
Method SendSignaletique(Token As %String) { //set UrlPats = "https://app.depistagesurdite.be/externaldemo" /*set internalID = "123456789" set lastName = "Test" set firstName = "CHRV" set dateOfBirth = "2022-05-04" set gender = "1" set clinicId = 22 set mothersName = "CHR VERVIERS" set address = "Rue du parc , 29" set postalCode = 4800 set place ="Verviers" set telephone1 = "087212824"*/ try { set JsonArray = ##class(%DynamicArray).%New() set SignaletiquePat = ##class(%DynamicObject).%New() set SignaletiquePat.internalID = "050522001" set SignaletiquePat.lastName = "Tata" set SignaletiquePat.firstName = "Silva" set SignaletiquePat.dateOfBirth = "05/05/2022" set SignaletiquePat.gender = "1" set SignaletiquePat.clinicId = 22 set SignaletiquePat.mothersName = "Anne Dede" set SignaletiquePat.address = "Rue des prés, 50" set SignaletiquePat.postalCode = "4620" set SignaletiquePat.place = "fléron" set SignaletiquePat.telephone1 = "0499998855" set JsonArray."0" = SignaletiquePat set JsonArrayOBJ = JsonArray.%ToJSON() $$$TRACE("Json : " _JsonArrayOBJ) set HTTPRequestPat = ##class(%Net.HttpRequest).%New() set HTTPRequestPat.ContentType = "application/json" Do HTTPRequestPat.SetHeader("Authorization","Bearer "_Token) Do HTTPRequestPat.EntityBody.Write(JsonArrayOBJ) // like this ? didnt work set ..Adapter.HTTPServer = "185.36.164.222" set ..Adapter.URL = UrlPats_"/api/patient/signaletic" set st = ..Adapter.SendFormDataURL(..Adapter.URL,.callResponsePat,"POST",HTTPRequestPat,,JsonArrayOBJ) // not working too.... #dim callResponsePat as %Net.HttpResponse set dynamicObject = {}.%FromJSON(callResponsePat.Data) set JsonString = dynamicObject.%ToJSON() $$$TRACE("return : " _ JsonString) set Iterator = dynamicObject.%GetIterator() While Iterator.%GetNext(.key,.val) { if (key = "Message") { if (val = "An Error as Occured.") { $$$TRACE("Erreur : "_ val) } else { $$$TRACE("pas d'erreur : " _val) } } } } catch ex { $$$TRACE("Error while sending datas: "_ex.DisplayString()) } }
Here are the logs :
Trace 2022-05-05 16:34:14.363 END 108271 Trace 2022-05-05 16:34:14.363 ERROR: Une erreur s’est produite. 108270 Trace 2022-05-05 16:34:14.363 JSON return : {"Message":"Une erreur s’est produite."} 108269 Trace 2022-05-05 16:34:14.363 HTTP status : 500 Trace 2022-05-05 16:34:14.300 Json That need to be send : [{"internalID":"050522001","lastName":"Tata","firstName":"Silva","dateOfBirth":"05/05/2022","gender":"1","clinicId":22,"mothersName":"Anne Dede","address":"XXXX","postalCode":"XXXX","place":"XXXX","telephone1":"XXXXXX"}]
Do you have any ideas ?
Thanks you.
Vessiere Thomas.
Product version: IRIS 2021.2
Try removing the JsonArayOBJ argument from your call to SendFormDataURL.
Hi i think i already tried and it didnt work
Your code:
Try this instead:
Hi.
Its already a Json :
Apologies! Missed that line earlier! Your method of making the JSON is different than how I do it though - and execute it successfully - so could try this?
Thanks. I did it but it didnt work :/
Methods that return a %Status don't automatically throw an exception. You have to check if the %Status is an error and throw it yourself. After your SendFormDataURL call, you might want to add the following and see if that gives you any more information:
However given that the HTTP status of the response is a 500 (an internal server error) there may also be a problem on that end.
Ok i will try with a check of an error.
However the webservice provider told me he got an empty request everytime...
Same error : HTTP 500 from the server... I guess its a empty body request again :
Here is the update code :
My Body is not empty..... i dont know anymore lol.
Sounds like the vendor is giving you bad information then. They need to be producing better error output on their side instead of just {'Message': 'An error has occurred.' }
Likely something in the formatting of what of the fields in the JSON package is wrong - incorrect field name or bad value - but without some better error messages from the vendor or more guidance from their end, you're kind of stuck.
Hey !
I found what was the problem.... Look a this, i used Postman as proxy for my request.
I found this issue with ''é"
.png)
Then i replace "é" with "e" and it worked !
Do you know if studio has spécial encoding character ?
How can i fix this ?
Thanks.
Or is this the %ToJSON() that failed to translate correctly ?
Solved.
Fixed with ZCONVERT :
Like the doc said for .%ToJSON :
https://docs.intersystems.com/irislatest/csp/documatic/%25CSP.Documatic....