[Resolved] Help with URL encoding

Hey all,

I’m trying to get Corona to talk to Parse via the REST API. It should be fairly simple but I’m missing something. The API looks like this…

curl -X GET \  
 -H "X-Parse-Application-Id: ID\_KEY" \  
 -H "X-Parse-REST-API-Key: API\_KEY" \  
 -G \  
 --data-urlencode 'username=testUser' \  
 --data-urlencode 'password=testPass' \  
 https://api.parse.com/1/login  

What’s the right way to make this GET request from Corona?

[code]
local function login ()

– ?? I tried various versions of url.escape on the parameters but I’m obviously not getting the format

baseUrl = “https://api.parse.com/1/login

network.request( baseUrl, “GET”, networkListener, params)

end
[/code] [import]uid: 1560 topic_id: 24102 reply_id: 324102[/import]

Looks like your web service endpoint uses custom HTTP headers for authentication (X-Parse-Application-Id and X-Parse-REST-API-Key) did you pass them in as headers?

This here shows an example on how to pass in http headers: http://developer.anscamobile.com/reference/index/networkrequest

For the query parameters (username/password), you could just append to the URL and URL encode them (https://api.parse.com/1/login?username=testuser&password=testPass). Your example code does not have any query parameters or HTTP headers specified, so I don’t think it could work.

Also you should check if your service supports to pass in username/password in HTTP body or Basic Auth HTTP header, that is nicer then in the URL

–wunderwuzzi

[import]uid: 118947 topic_id: 24102 reply_id: 97227[/import]

You may still need to encode the strings (takes out spaces, converts certain characters to a binary-safe value). Here are the urlencode/urldecode functions that I use:

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  
  
function urldecode(str)  
 str = string.gsub (str, "+", " ")  
 str = string.gsub (str, "%%(%x%x)",  
 function(h) return string.char(tonumber(h,16)) end)  
 str = string.gsub (str, "\r\n", "\n")  
 return str  
end  

[import]uid: 19626 topic_id: 24102 reply_id: 97235[/import]

Thanks for the suggestions. Yes, I’m passing the custom headers. I’ve been trying variations of URL encoding, using url.escape and the urlencodefunction.

I’m guessing the problem is either the username/password string just comes out with an extra slash somewhere or there’s a missing authentication header. I’ve tried username/password as a JSON object but that doesn’t work either.

This is what hasn’t worked…perhaps there should be no JSON header for the GET it’s just passing a string?

[code]
headers[“Content-Type”] = “application/json”
headers[“X-Parse-Application-Id”] = “TK” – your Application-Id
headers[“X-Parse-REST-API-Key”] = “TK” – your REST-API-Key

local function login (message)

baseUrl = “https://api.parse.com/1/login/
message = escape(“username=testUser&password=testPass”)
–also tried urlencode (string)
params.body = message

network.request( baseUrl, “GET”, networkListener, params)

end
[/code] [import]uid: 1560 topic_id: 24102 reply_id: 97239[/import]

The curl example provided uses -G, which means the username/password is appended as query parameters (just a GET request)[1]. Maybe the services doesn’t support username/password in the body? Try to append it in the URL as query params and see if that works.

–wunderwuzzi
[1]
http://curl.haxx.se/docs/manpage.html

-G, --get

When used, this option will make all data specified with -d, --data or --data-binary to be used in a HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a ‘?’ separator. [import]uid: 118947 topic_id: 24102 reply_id: 97247[/import]

Thanks much. I got it to work. The problem was an incorrect header. I removed the JSON header and used Content-Type = application/x-www-form-urlencoded and it worked.

FWIW, http://.www.hurl.it was very helpful in diagnosing the issue. [import]uid: 1560 topic_id: 24102 reply_id: 97319[/import]

Great, glad to hear that you got it to work!

–wunderwuzzi [import]uid: 118947 topic_id: 24102 reply_id: 97360[/import]