Using system.file.writeFile to save a PNG

I’m calling an API to get a png in a python script. Then saving it to my computer using system.file.writeFile.

I am confident that the URL I am using is correct, because I have also used system.net.openURL to download the same PNG with my web browser.

Both resulting files are similar in size (both say 51k), but the GET file appears to have additional characters in it when compared with a text editor (notepad++).

I suspect the problem is the encoding part of the writeFile function. Is there an encoding that would work best for PNG, or BLOB files? Or what encodings are available? The user manual does not specify all of them. I’m trying all the ones mentioned.

OR is there a different function to use to write bytes to a file?

Here is part of my code:

png = system.net.httpGet(pngURL, headerValues=headerValues, usesCaches=0)

system.net.openURL(pngURL)

system.file.writeFile(filePath + ".png", png, 0, "ISO-8859-1")

Don’t use system.net.httpGet, use system.net.httpClient.

The old functions assume you’re GETing String data. You can’t turn binary into a UTF-8 encoded String and back into binary and expect it to survive unmodified.

1 Like

Thanks Kevin. Additional breadcumbs for the next guy (or gal)…

after the getting a response, I also had to make sure I saved only the body of the response: here is my code snippet as modified:

client = system.net.httpClient()

#png = system.net.httpGet(pngURL, headerValues=headerValues, usesCaches=0)
png = client.get(pngURL)

print png

system.net.openURL(pngURL)

system.file.writeFile(filePath + ".png", png.body)