Rochdi Badis · May 28


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()
   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=""
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)
Set tSc=Httprequest.Post("/API/SENSORS/VIBRATION")
Set StateCode=Httprequest.HttpResponse.StatusCode
Set State=Httprequest.HttpResponse.StatusLine
Quit StateCode_"|"_State



Product version: Ensemble 2014.1
0 196
Discussion (10)1
Log in or sign up to continue

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

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()?


just after

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

As with any other object

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?


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.

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


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) }


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

Thanks guys, CopyFrom saved the days 

Cheers all