"Invalid Parameter: URL argument was malformed URL" in network.request()

When I use

network.request()

I see my console log showing this error message:

Invalid Parameter: URL argument was malformed URL

But I don’t see any callback to my listener. I mean I can’t know this error is happening when/after calling network.request()

Is it possible to know? so I can handle things for the error?

Thanks.

Is this a different issue that what you posted here?  http://forums.coronalabs.com/topic/39150-why-is-this-a-invalid-parameter-url-argument-was-malformed-url/

It’s a different topic.

I mean even if the URL is malformed (the other post is asking why it is malformed), should the listener get something? Otherwise, how could I handle the error at runtime? (because the URL is dynamically retrieved from an outside source, not hard coded.)

You’re going to have to post come code along with the URL that is malformed. Are you printing the value of the URL?

local function downloadListener( event ) if (event.response == nil) then print("event.response is nil") -- some error handling return end if ( event.isError ) then print( "download error" ) -- some error handling return end -- some handling for the download end local params = {} params.timeout = 10 network.download(theURL, "GET", downloadListener, params, myFilenamePrefix..data.id, system.TemporaryDirectory )

theURL is not a hardcoded URL, but a URL retrieved from a 3rd party API during run time. It varies every time. In 99% cases, it runs perfect. Only when the URL is considered malformed, I don’t get any callback, hence I can’t do further handling (for example, showing a message to the user that the download is failed)

FYI, the URL is considered malformed: 

http://cdn.unwire.hk/wp-content/uploads/2013/09/Xiaomi2013-Keynote-PDF-20130905.pdf(頁面_21∕215)-590x382.jpeg

The question is, when a URL is considered invalid by network.download(), would there be any way to be notified about the error?

Is this a different issue that what you posted here?  http://forums.coronalabs.com/topic/39150-why-is-this-a-invalid-parameter-url-argument-was-malformed-url/

It’s a different topic.

I mean even if the URL is malformed (the other post is asking why it is malformed), should the listener get something? Otherwise, how could I handle the error at runtime? (because the URL is dynamically retrieved from an outside source, not hard coded.)

You’re going to have to post come code along with the URL that is malformed. Are you printing the value of the URL?

local function downloadListener( event ) if (event.response == nil) then print("event.response is nil") -- some error handling return end if ( event.isError ) then print( "download error" ) -- some error handling return end -- some handling for the download end local params = {} params.timeout = 10 network.download(theURL, "GET", downloadListener, params, myFilenamePrefix..data.id, system.TemporaryDirectory )

theURL is not a hardcoded URL, but a URL retrieved from a 3rd party API during run time. It varies every time. In 99% cases, it runs perfect. Only when the URL is considered malformed, I don’t get any callback, hence I can’t do further handling (for example, showing a message to the user that the download is failed)

FYI, the URL is considered malformed: 

http://cdn.unwire.hk/wp-content/uploads/2013/09/Xiaomi2013-Keynote-PDF-20130905.pdf(頁面_21∕215)-590x382.jpeg

The question is, when a URL is considered invalid by network.download(), would there be any way to be notified about the error?

Was there ever a solution to this?  I’m running in to the same problem.   Grabbing URLs from a list and most are fine, but when they happen to be malformed, there’s no error to the listener, they just fail.

URL’s need to be URL encoded.  Perhaps you’re running into that issue.  See:

http://coronalabs.com/blog/2014/11/11/tutorial-encoding-urls-for-network-usage/

Rob

Well yes, but reasonably should the event.isError say that?

The problem I have, as with the original poster, is using a database of previously URL encoded urls – where, obviously, a couple URLs aren’t correctly encoded.   You can’t easily re-encode a full URL.  So because no error is returned, you’re basically saying we would need to pick apart the query string of URL, de-encode it, re-encode and put it back together.

Which, from the perspective of Corona being there to make things easier – well, isn’t very easy.  It prints to the console “Invalid Parameter: URL argument was malformed URL” – but there’s nothing we can do with that from the console.  If only it would return that same message in “event.isError”, we could handle it and move on (in my case, that would mean ignoring that URL and moving to the next in the list).

Make sense?

We are frequently at the mercy of what the Operating System gives us.  Perhaps the OS doesn’t trigger it’s call back because it never tries the URL.  In that case perhaps we should return something from network.request() like:

local success, message = network.request(…)

if not success then

    print( message )

end

But I don’t even know if that’s possible.  I seriouslly suspect that the error is happening before the request is made and that’s why it’s failing to trigger the call back.

Corona does try to make things easier and given infinite resources we probably could tackle every thing, but as of now, it’s your responsibility to urlencode each query parameter.  When you get the data from the database, if your app expects to use it, it’s got to parse it into Lua variables in the first place. 

Rob

Yeah, I’d be fine with ANYTHING more than a message to the console that can seemingly only be used by me visually seeing it in the corona simulator console. :wink:  

Of course I did try getting a message result directly from the call itself – but indeed, that doesn’t return anything in either success or fail.  Same goes for the loadRemoteImage call.  It returns nil in every case.

When you get the data from the database, if your app expects to use it, it’s got to parse it into Lua variables in the first place. 

Which is just/should be just populating a table. :wink:

Anyway – given this… Any thoughts on how one could check their own URLs?  The more I think about it, the more trouble I have.

If I have 2 URLs, one that is already URL encoded and one that is not … How do I check to see which is which – encoding an already encoded URL isn’t feasible.  I’m trying to think through dissecting a URL and checking the parts but perhaps that exists somewhere?

For example:

http://www.Google.com/?q=something with a / is confusing?

http://www.Google.com/?q=something%20with%20a%20%2F%20is%20confusing%3F

You could use a string.match() to look for the presence of a percent sign followed by a number.

if string.match(URL, “%%%d”) then

    – has been URL encoded

else

    – has not been

end.

I’ve not tested it, but something like that should work.  The only gotcha is if there are only spaces and they were URL encoded to +'s, then you might want to do a second check.  If your URL encode routine is turning spaces into %20’s then you would be okay with that test.

http://www.Google.com/?q=something

would fail that test…  and then I have to disassemble and reassemble the variations of URLs… those with ? query strings and those with them buried in a standard looking url.

It also leaves the issue of encoding a url.

For example, you can’t just urlencode http://www.Google.com/?q=something … cause that gives you 

http%3A%2F%2Fwww.Google.com%2F%3Fq%3Dsomething

 

You’re going to have to split off the query string regardless.  You can’t pump the whole URL through URL encode.   I know we posted a string recipe tutorial a year or so a go.  There are a ton of string.split() functions that can be found on google, or you can always do it with string match too.

Rob

… Right, which reinforces the frustration of seeing the URL error in the console and having no way to get that inside the app code itself. :wink:

I’m bringing in the URLs via json from a remote server running PHP … Given that, I found this function seems to work to sanitize the URLs before sending them to the corona app…  Just posting it should anyone else get here from a search. :slight_smile:

function LOCAL\_FIX\_URL ($url) {    $encoded\_url = preg\_replace\_callback('#://([^/]+)/([^?]+)#', function ($match) {         return '://' . $match[1] . '/' . join('/', array\_map('rawurlencode', explode('/', $match[2])));         }, $url);    return $encoded\_url;    }