Question
· 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
Discussion (10)1
Log in or sign up to continue

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ž