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?
S 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)
D 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
Httprequest.EntityBody is a %GlobalBinaryStream object.
In your code I see
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ž
Just an idea:
wouldn't CopyFromAndSave(source As %Stream.Object) be a better approach ??
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
Social networks
InterSystems resources
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue