Http Requests, Xml, And A Malformed Url...

Trying to connect my app to USPS and their Rate API to return running rates on packages. To test on their server, I have a dummy XML I’m passing to see if I can get any response, but Corona is saying that the URL I’m passing is malformed. Anyone have any thoughts?

 

[lua]

 

local path = system.pathForFile( “req_usps.xml”, system.ResourceDirectory )

local request = io.open( path, “r” )

 

local postData = request:read( “*a” )

 

local function networkListener( event )

if ( event.isError ) then

print( “Network error!”)

else

print ( "RESPONSE: " … event.response )

end

end

 

local target = “http://production.shippingapis.com/ShippingAPI.dll?API=RateV4&XML=” … postData

print( target )

 

network.request( target, “GET”, networkListener )

[/lua]

 

And the error…

 

2013-03-14 11:36:09.906 Corona Simulator[16116:707] http://production.shippingapis.com/ShippingAPI.dll?API=RateV4&XML=<?xml version="1.0" encoding="UTF-8" ?>2FIRST CLASSPARCEL2290333101260REGULARtrue

 

2013-03-14 11:36:09.906 Corona Simulator[16116:707] Invalid Parameter: URL argument was malformed URL

 

Warning: /Users/kmusngi/Development/USPS_req_test/main.lua:23: URL argument was malformed URL

 

 

 

 

I am having the same problem. Funny thing is, this work well on iPhone, and also Corona Simulator (iPhone skin and Droid skin, and Sensation skin), but for Nexus skin, it shows this error. wth?

You are very likely not going to be able to send XML as a GET request.  If it were to work, the XML would need to be URL encoded.  All those brackets, equals, quotes, spaces and stuff will fry a URL string.  Secondly, GET has a limited amount of data that can be passed (usually in the 2K-8K range for most browser and server configs, but some combos could be in the 255 character range).  This would likely be much better as a POST request  instead.

But for certain, URL encode your string:

[lua]

  function urlencode(str)
      if (str) then
          str = string.gsub (str, “\n”, “\r\n”)
          str = string.gsub (str, “([^%w])”,
          function ( c ) return string.format ("%%%02X", string.byte( c )) end)
          str = string.gsub (str, " ", “+”)
      end
      return str    
end

local target = “http://production.shippingapis.com/ShippingAPI.dll?API=RateV4&XML=” … urlencode(postData)

[/lua]

or something like that.

I am having the same problem. Funny thing is, this work well on iPhone, and also Corona Simulator (iPhone skin and Droid skin, and Sensation skin), but for Nexus skin, it shows this error. wth?

You are very likely not going to be able to send XML as a GET request.  If it were to work, the XML would need to be URL encoded.  All those brackets, equals, quotes, spaces and stuff will fry a URL string.  Secondly, GET has a limited amount of data that can be passed (usually in the 2K-8K range for most browser and server configs, but some combos could be in the 255 character range).  This would likely be much better as a POST request  instead.

But for certain, URL encode your string:

[lua]

  function urlencode(str)
      if (str) then
          str = string.gsub (str, “\n”, “\r\n”)
          str = string.gsub (str, “([^%w])”,
          function ( c ) return string.format ("%%%02X", string.byte( c )) end)
          str = string.gsub (str, " ", “+”)
      end
      return str    
end

local target = “http://production.shippingapis.com/ShippingAPI.dll?API=RateV4&XML=” … urlencode(postData)

[/lua]

or something like that.