Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Have a problem with using in a string with '{' for a network request
Started by thereefgames Apr 11 2017 08:13 AM

2 replies to this topic
network request

Best Answer Rob Miracle , 11 April 2017 - 09:05 AM

Welcome to the Forums!

 

You need to be able to "URL encode" your parameters. 

local 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

You only need to encode the values, not the whole URL.

local myEvents = "https://api.mlab.com/api/1/databases/xxx/collections/events?apiKey="..urlencode(myKey).."&q=" .. urlencode("{'id':'SMASH'}")

or something like that.

 

Rob

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

thereefgames

[GLOBAL: userInfoPane.html]
thereefgames
  • Observer

  • 16 posts
  • Corona SDK

Hi,

 

Just got a mac and installed coronasdk. I used coronasdk on windows all these while. I tried to launch a program I wrote and worked well in windows but coronasdk on mac, yelled back at me.

 

My code sniplet for restful 

 

...

local function getDocs(event)

  ... 

end

 

network.request(myEvents,"GET",getDocs)
 
The simulator gives me this error
2017-04-12 00:07:56.910 Corona Simulator[658:16177] ERROR: network: todayView.lua:346: invalid parameter: URL argument was malformed URL: https://api.mlab.com/api/1/databases/xxx/collections/events?apiKey=Sjduh67kjkjz&q={'id':'SMASH'}
 
but if I replace the { with %7B and } with %7D, it works fine
 
On windows coronasdk, it works perfectly. 
Anyone has any idea? I am pretty new to MAC and coronasdk on MAC
 
Thanks
Rgds
Limakoh
 
 

 



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,072 posts
  • Enterprise

  Best Answer

Welcome to the Forums!

 

You need to be able to "URL encode" your parameters. 

local 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

You only need to encode the values, not the whole URL.

local myEvents = "https://api.mlab.com/api/1/databases/xxx/collections/events?apiKey="..urlencode(myKey).."&q=" .. urlencode("{'id':'SMASH'}")

or something like that.

 

Rob



[TOPIC: post.html]
#3

thereefgames

[GLOBAL: userInfoPane.html]
thereefgames
  • Observer

  • 16 posts
  • Corona SDK

HI Rob

Thanks a lot for the advice. I will implement that. 

Sincerely

Limakoh




[topic_controls]
[/topic_controls]