Have a problem with using in a string with '{' for a network request

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

local myEvents = "https://api.mlab.com/api/1/databases/xxx/collections/events?apiKey="…myKey…"&q={‘id’:‘SMASH’}"

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

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

HI Rob

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

Sincerely

Limakoh

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

HI Rob

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

Sincerely

Limakoh