Rocket U2 | UniVerse & UniData

 View Only

 Program issue while downloading an image from an URL

Jump to Best Answer
Kevin Garde.'s profile image
Kevin Garde. posted 08-09-2022 06:58
Good morning/night!,
So I have this little issue when running this little service I'm doing to download files via URL, text part is working correct but when I try to download an image, the image gets corrupted or something because when you open it, it has like a dizzy effect or its green and red directly.
I stracted a little code to test the image download which is the following:
   URL='http://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png'
   DIR.RESPUESTA='COMGATEWAY'
   NOM.FICH.RESP='logo.png'
   HTTP.STATUS=''
   TIME.OUT='1000'
   DATOS.RESPUESTA=''
   CABECERA.RESPUESTA=''

   RESPUESTA=createRequest(URL,'GET',HTTP.HANDLER)
   RESPUESTA=submitRequest(HTTP.HANDLER,TIME.OUT,'',CABECERA.RESPUESTA,DATOS.RESPUESTA,HTTP.STATUS)
   CRT 'KG.HTTP: HTTP.STATUS [':HTTP.STATUS:'] ':TIME()

* Checks HTTP status
   IF HTTP.STATUS<1,1>#'200' THEN
* Msg 'La respuesta del servidor no ha sido correcta. %1'
      CALL GENMENSAJE(64746,HTTP.STATUS<1,1>,'T',NOM.SUB,MSG.ERROR)
   END

* WRITES THE DATA IN THE FILE logo.png
   IF DATOS.RESPUESTA#'' THEN
      CALL QOPEN(DIR.RESPUESTA,FICHERO,'')
         WRITE DATOS.RESPUESTA ON FICHERO,NOM.FICH.RESP
      DATOS.RESPUESTA=''
   END​

This is the output:

KG.HTTP: HTTP.STATUS [200²OK] 45952.5254


Also it is returning this hearder:

KG.HTTP: CABECERA.RESPUESTA [Accept-Ranges²bytes¦Content-Type²image/png¦Cross-Origin-Resource-Policy²cross-origin¦Cross-Origin-Opener-Policy-Report-Only²same-origin; report-to="static-on-bigtable"¦Report-To²{"group":"static-on-bigtable","max_age":2592000,"endpoints":[{"url":"https://csp.withgoogle.com/csp/report-to/static-on-bigtable"}]}¦Content-Length²3501¦Date²Tue, 09 Aug 2022 10:57:29 GMT¦Expires²Tue, 09 Aug 2022 10:57:29 GMT¦Cache-Control²private, max-age=31536000¦Last-Modified²Tue, 22 Oct 2019 18:30:00 GMT¦X-Content-Type-Options²nosniff¦Server²sffe¦X-XSS-Protection²0] 46649.6447



An this is the image:
(The backgroud is edited and changed to this blue color you can see, because the original backgroud is white and the letters are also white)
Its possible that I'm missing something in the request maybe?,  Could be that the WRITE of the data output is writing something wrong because of the encoding or something that I'm not controling / sending in the request?
Would appreciate your help,
Best regards!

John Jenkins's profile image
John Jenkins Best Answer
Kevin,

If using UniData see 'NOCONVERT', otherwise if UniVerse then use WRITEBLK not WRITE.

From the UniData BASIC reference documentation:
  • Warning: Do not use UniBasic READ and WRITE commands to open or modify binary data in DIR type files (for example, BP). Doing so can corrupt data in the file. Instead, use OSREAD or OSBREAD after executing the UniBasic NOCONVERT command.
The closest approximation in UniVerse is I believe READBLK (from the UniVerse BASIC reference guide)
  • Note that READBLK is a byte level operation and returns a string in which each character will represent one byte from the file and hence is always in the range char(0) to char(255). When used with the ENCODING clause, the bytes value is the number of bytes to be read, not the character count in the decoded data.


Hoping this helps.

Regards,

John Jenkins
Kevin Garde.'s profile image
Kevin Garde.
Hello John,
Thanks for the tip, I checked the documentation and as you said WRITEBLK worked perfectly for me, I cant tell you how I didn't see it since I looked the docu a hundred times haha.
Thanks for your answer! :)

Best regards,

Kevin Gardeazabal (a.k.a Kevin Garde.)