Getting Different HTTP response across OS

I’m running a script using

url = http://192.168.1.2:8096/main/file.ext
content = system.net.httpGet(url)

when I print the content on Windows OS I’m getting a result (cropped)

u'RIFF\xf4\x7f\x07\x00WA'

BUT, when I run on Ubuntu, I’m getting something different (cropped)

u'RIFF\ufffd\x7f\x07\x00WA'

Does system.net.httpGet() depends on the OS on how it encodes data?

Looks like system.net.httpGet() uses the default system encoding, which is different between Windows and *nix.

1 Like

Yes, ah Kathy beat me to it.

1 Like

Since you’re on 8.x try system.net.httpClient() instead.

1 Like

great thanks a lot!,
now, how could I get the same output that I was having BUT with system.net.httpClient(). So fair I have:

client = system.net.httpClient()
response = client.get('http://192.168.1.2:8096/main/file.ext')
text = response.getText()

returns empty unicode

That explains a lot!
Is it possible to set the enconder with system.net.httpGet()?
Is “chunked” the default enconder name (I don’t even know if that exists) for Windows?

What about printing the response object itself or response.getBody()?

1 Like

also empty :thinking:

array('b')

Where are you seeing a reference to “chunked”? That probably refers to HTTP chunked transfer encoding… not the encoding of some content string.

1 Like

No, but it is with system.net.httpClient(), if the response doesn’t indicate a charset; you can pass a charset name (i.e. "UTF-8") to the response.getText() method.

Windows (on most locales, anyways) defaults to ISO-8859-1.

Can you post your exact script that you’re using with httpClient()?

1 Like

I saw that when I got the headers. Oh ok, thanks for the explaining.

sure

client = system.net.httpClient()
response = client.get('http://192.168.1.2:8096/main/file.ext')
response = response.getText()

I wouldn’t expect getText() to be what you want if you’re requesting a .wav file; .getBody() is probably more useful. It’s also possible that this target service (an Ignition gateway? Something else?) is returning a redirect or some other partial response that httpClient isn’t following; you should inspect the response object (status code, etc).

1 Like

Yes the response is from Ignition Gateway
I just wish to return the response and then use system.file.writeFile(path, file). But this functions needs a byte [ ] type file

Try this instead:

client = system.net.httpClient(redirect_policy="ALWAYS")

response.getBody() will be the byte[] you need to actually write the file. This was never going to work with you turning the binary data into a String first, regardless of encoding.

2 Likes

thanks a lot, I ended up using pure Jython and non Ignition build in function. BUT I will definitely try that.

Great, this is returning the same response across OS!
Good for us that the server is 8.1.x.
What was happening? Who was blocking the download, the client instance? or the Ignition Server was not allowing access to the file while in NORMAL Redirect Policy?

I did inspect it. I got ‘OK’ for the status and 200 for the code. So, I assumed the problem was related to the encoding. Apparently not.

I think our docs are wrong and the default redirect policy is actually NEVER.

I’m surprised you were getting a 200 status code. I would have expected a 302 redirect since you are including “main” in the URL, which is no longer necessary but continues to work via redirect.