Written by

Question Nezla · May 28, 2022

Write JSON file in HTTPREQUEST

Hi Guys,

how can I write a json file in httprequest, I'm using the below but when I check what's in mystring it's actually truncated and doen't include all my file?

 Set stream=##class(%FileBinaryStream).%New()
    FilePath="D:\Allied_InterfaceData\ResponseFiles\CollectionData\CollectionGood\CollectionGood_9431312.json"
   Set File = ##class(%File).%New(FilePath)
Do File.Open("R")
Set Line = File.Read(1000)
While (File.Read(1000)'="") {S mystring=mystring_File.Read(1000)
} 
  Set Httprequest=##class(%Net.HttpRequest).%New()
Set Httprequest.SSLConfiguration="RTLS"
Set Httprequest.Server="vibra-api-dev.azurewebsites.net"
Set Httprequest.Timeout=30
Set Httprequest.Https=1
set Httprequest.ContentType="application/json"
Do Httprequest.SetHeader("Accept","application/json")
Do Httprequest.SetHeader("Accept-Language","en_US")
    Do Httprequest.SetHeader("Authorization","Bearer "_^Token)
  Httprequest.EntityBody.Write(mystring)
Set tSc=Httprequest.Post("/API/SENSORS/VIBRATION")
Set StateCode=Httprequest.HttpResponse.StatusCode
Set State=Httprequest.HttpResponse.StatusLine
Quit StateCode_"|"_State

Thanks

Product version: Ensemble 2014.1

Comments

Robert Cemper · May 28, 2022

Httprequest.EntityBody is a %GlobalBinaryStream object.
In your code I see

Do Httprequest.EntityBody.Write(mystring)

But no %Save()  to take care of completion and persistence of the object

0
Nezla  May 28, 2022 to Robert Cemper

So should I me converting my file into a %GlobalBinaryStream ? how can Correct my code because I'm not sure where or how to use %Save()?

thanks

0
Robert Cemper  May 28, 2022 to Nezla

just after

Do Httprequest.EntityBody.Write(mystring)
Do Httprequest.EntityBody.%Save()

As with any other object

0
Nezla  May 28, 2022 to Robert Cemper

I mean, just to put it simple and foget my code, how would you a post HTTPREQUEST for a JSON file given a file path?

Thanks 

0
David Hockenbroch  May 31, 2022 to Nezla

Rochdi, I'd get rid of:

Set Line = File.Read(1000)
While (File.Read(1000)'="") {S mystring=mystring_File.Read(1000)
} 

And then after your set Httprequest = ##class(%Net.HttpRequest).%New(), use:

set sc = Httprequest.EntityBody.CopyFromAndSave(File)

Then you can check sc to see if you got any errors doing that.

0
Oliver Wilms · May 28, 2022

How long is mysring? How many characters are in the file?

0
Matjaz Murko · May 28, 2022

Hi.

In this part of the code:

Set Line = File.Read(1000)
While (File.Read(1000)'=""{ S mystring=mystring_File.Read(1000)

you are reading first 1000 chars which are not included in mystring and again in each loop checking the while condition you are reading 1000 chars and this chars are not included in mystring.

It should be:

//Set Line = File.Read(1000)
Set mystring = ""
While ('File.AtEnd{ S mystring=mystring_File.Read(1000) }

Regards,
Matjaž

0
Matjaz Murko  May 28, 2022 to Robert Cemper

I was just trying to point out the cause of the problem. There are many ways to do the same job :)...

0
Nezla · May 31, 2022

Thanks guys, CopyFrom saved the days 

Cheers all

0