Network.request() silent failure

local function downloadListener( event ) if ( event.isError ) then print( "Network error - download failed: ", event.response ) elseif ( event.phase == "began" ) then print( "Progress Phase: began" ) elseif ( event.phase == "ended" ) then print( "Displaying response image file" ) print("File name: /"..event.response.filename) cameraImage = display.newImage( event.response.filename, event.response.baseDirectory, 64, 36 ) cameraImage.x = display.contentCenterX -- The code crashes here as the image is nil cameraImage.y = display.contentCenterY-100 cameraImage.alpha = 0 transition.to( cameraImage, { alpha=1.0 } ) end end local function changeCamera() local params = {} params.progress = "download" params.response = { filename = "corona.jpg", baseDirectory = system.DocumentsDirectory } print(system.DocumentsDirectory) network.request( "\<URL\>", -- Can't include the actual URL for security purposes "GET", downloadListener, params ) end&nbsp;

I’m having a problem with network.request(), in that it’s silently failing to download a file. 
 
A test url provided works just fine; http://docs.coronalabs.com/images/simulator/image-mask-base2.png 
 
The main differences are that the url is using basic authentication (which according to this tutorial, corona is capable of: https://docs.coronalabs.com/tutorial/social/connectREST/index.html) notably with an empty password, and that instead of accessing the file directly, the url links to a .cgi script that gives the file.
 
The URL (example): http://<username>:@<server>:<port>/image/jpeg.cgi
 
Tested in any web browser, this url displays the image. However, when I try to use network.request() to GET the image, it throws no errors but also grabs nothing, that is until it tries to use the non-existent image. It also doesn’t write to any file. The code in question is provided.

My real question is, what might be happening, and what can i do to get more verbose info about what’s going on behind the scenes?

What version of Corona are you using and is this a test in the simulator and/or on a device?  If on simulator OS X or Windows?

Also, is there any way you can make a MICRO functional test case for us to run and let us download the source/project?  I’ve looked at your code, but it is very specific to your usage and impossible for us to test unless we have a similar setup.

I know this may be a bit of a problem since I’m essentially asking you let us see the user name and password.

Simulator, 2017.3184 (2017.12.8). Windows

I’m afraid I can’t do that, because this exact test case happens in a device in an internal network, not something I can recreate. (For example, this feature would only work if the phone was connected to the wifi network)

To give you an example of what i’m looking for, if I could give you a working funtional test of the code, and it didn’t work, what might you try? What would you look for? 

With network.request() not giving me any sort of output or error at all, I’m having trouble even beginning on a solution.

Hi,

What does your event.status and event.responseHeaders hold?

-dev

Yes! This is the kind of thing I was looking for.

Now the question is, why is network.response() getting a 401 unauthorized error?

I have tried the exact URL in every brower I can get my hands on, and with cURL from various locations, all of which worked perfectly.

Any clues as to what could be causing this?

Hi,

My guess is that for whatever reason, Corona is not handling the blank password properly. Have you tried this method https://docs.coronalabs.com/tutorial/social/connectREST/index.html#headers

It could also be a headers or mime type issue. Have you tried is using network.download?

The other thing you can do is use a tool like Postman and study the headers more deeply.

-dev

Hi,

Have you tried the url without the colon?

http://\<username\>@\<server\>:\<port\>/image/jpeg.cgi

-dev

Thank you so much for these bits of advice. I tried all four of these options:

http://<username>:@<server>:<port>/image/jpeg.cgi

http://<username>@<server>:<port>/image/jpeg.cgi

params.headers[“Authentication”] = mime.b64("<username>:")

http://<server>:<port>/image/jpeg.cgi

params.headers[“Authentication”] = mime.b64("<username>")

http://<server>:<port>/image/jpeg.cgi

With all four, I’m getting the same empty response and 401 status. These are the response headers as printed in the console:

16:11:02.200    Connection:

16:11:02.200          close

16:11:02.200    Content-Length:

16:11:02.200          0

16:11:02.200    Date:

16:11:02.200          Fri, 27 Apr 2018 20:11:02 GMT

16:11:02.200    WWW-Authenticate:

16:11:02.200          Basic realm=“DCS-4701E”

16:11:02.200    HTTP-STATUS-LINE:

16:11:02.200          HTTP/1.0 401 Unauthorized

16:11:02.200    Content-Type:

16:11:02.200          text/html; charset=ISO-8859-1

Hi,

Have you tried:

params.headers["Authentication"] = mime.b64("\<username\>:") http://\<server\>:\<port\>/image/jpeg.cgi

You could also try adding a space where the password should be:

params.headers["Authentication"] = mime.b64("\<username\>: ") http://\<server\>:\<port\>/image/jpeg.cgi

Seems strange it’s giving your so much trouble.

-dev

Yes, sorry that was a typo. Still unauthorized. Again, only corona seems to be having this problem.

Hi,

Without some type of example link it is hard to provide any more ideas. It’s rare that you wouldn’t be able to pull the request if you can get it on the browser. It is usually a matter of headers like content-type, user-agent, etc., something the script is expecting that is default via a browser call.

I usually use Postman to figure these things out, or if the cgi script you are using is open-source I could put it on my server and test, but beyond that I’m out of options for you.

-dev

What version of Corona are you using and is this a test in the simulator and/or on a device?  If on simulator OS X or Windows?

Also, is there any way you can make a MICRO functional test case for us to run and let us download the source/project?  I’ve looked at your code, but it is very specific to your usage and impossible for us to test unless we have a similar setup.

I know this may be a bit of a problem since I’m essentially asking you let us see the user name and password.

Simulator, 2017.3184 (2017.12.8). Windows

I’m afraid I can’t do that, because this exact test case happens in a device in an internal network, not something I can recreate. (For example, this feature would only work if the phone was connected to the wifi network)

To give you an example of what i’m looking for, if I could give you a working funtional test of the code, and it didn’t work, what might you try? What would you look for? 

With network.request() not giving me any sort of output or error at all, I’m having trouble even beginning on a solution.

Hi,

What does your event.status and event.responseHeaders hold?

-dev

Yes! This is the kind of thing I was looking for.

Now the question is, why is network.response() getting a 401 unauthorized error?

I have tried the exact URL in every brower I can get my hands on, and with cURL from various locations, all of which worked perfectly.

Any clues as to what could be causing this?

Hi,

My guess is that for whatever reason, Corona is not handling the blank password properly. Have you tried this method https://docs.coronalabs.com/tutorial/social/connectREST/index.html#headers

It could also be a headers or mime type issue. Have you tried is using network.download?

The other thing you can do is use a tool like Postman and study the headers more deeply.

-dev

Hi,

Have you tried the url without the colon?

http://\<username\>@\<server\>:\<port\>/image/jpeg.cgi

-dev

Thank you so much for these bits of advice. I tried all four of these options:

http://<username>:@<server>:<port>/image/jpeg.cgi

http://<username>@<server>:<port>/image/jpeg.cgi

params.headers[“Authentication”] = mime.b64("<username>:")

http://<server>:<port>/image/jpeg.cgi

params.headers[“Authentication”] = mime.b64("<username>")

http://<server>:<port>/image/jpeg.cgi

With all four, I’m getting the same empty response and 401 status. These are the response headers as printed in the console:

16:11:02.200    Connection:

16:11:02.200          close

16:11:02.200    Content-Length:

16:11:02.200          0

16:11:02.200    Date:

16:11:02.200          Fri, 27 Apr 2018 20:11:02 GMT

16:11:02.200    WWW-Authenticate:

16:11:02.200          Basic realm=“DCS-4701E”

16:11:02.200    HTTP-STATUS-LINE:

16:11:02.200          HTTP/1.0 401 Unauthorized

16:11:02.200    Content-Type:

16:11:02.200          text/html; charset=ISO-8859-1

Hi,

Have you tried:

params.headers["Authentication"] = mime.b64("\<username\>:") http://\<server\>:\<port\>/image/jpeg.cgi

You could also try adding a space where the password should be:

params.headers["Authentication"] = mime.b64("\<username\>: ") http://\<server\>:\<port\>/image/jpeg.cgi

Seems strange it’s giving your so much trouble.

-dev

Yes, sorry that was a typo. Still unauthorized. Again, only corona seems to be having this problem.