Question
Rochdi Badis · May 28

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

thanks

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?

Thanks 

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?

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ž

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