LuaSocket - http.request (SOLVED)

Hey,

i am trying to trigger my Firebase authentication with HTTP and firebase REST API.
Because i wasnt able to get this (https://github.com/rokf/lua-firebase) to run, i decided to port it into LuaSocket.

This is how he does his authentication signup:

idtoolkit_reqcore("signupNewUser",{email=email,password=password,returnSecureToken=true},M.key)

local function idtoolkit_reqcore(appstr,t,key)
	local uri = string.format("https://www.googleapis.com/identitytoolkit/v3/relyingparty/%s?key=%s",appstr,key)
	local r = request.new_from_uri(uri)
	r.headers:upsert(":method","POST",false)
	r.headers:upsert("content-type","application/json",false)
	local datastring = cjson.encode(t)
	r.headers:upsert("content-length",tostring(#datastring),false)
	r:set_body(datastring)
	local h,s = r:go()
	if h:get(":status") ~= "200" then return nil, h:get(":status"), s:get_body_as_string() end
	return cjson.decode(s:get_body_as_string())
end

I used his code and the firebase reference (https://firebase.google.com/docs/reference/rest/auth/#section-create-email-password) to code the following:

function M.signup_email (email, password)
	local endpoint = "signupNewUser"	
	local url = string.format("https://www.googleapis.com/identitytoolkit/v3/relyingparty/%s?key=%s",endpoint, M.apiKey)
	local headers = {}  
	headers["content-type"] = "application/json"
	headers[":method"] = "POST"
	local datastring = myJson.encode({ email = email, password = password, returnSecureToken = true })
	local request = {url = url, method = "POST", headers = headers, post_data = datastring, callback = callback}
	pprint(request)
	M.result = http.request(request)
	pprint(M.result)
end 

My result just prints “1” and i dont know why.
My request looks like this if i print it:

{
  method = POST,
  url = https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=[MyApiKey],
  post_data = {"password":"testuser1220","email":"test@test.com","returnSecureToken":true},
  callback = function: 0x1a4b6390,
  headers = {
    content-type = application/json,
    :method = POST,
  }
}

Can anyone help me?

Are you actually using the HTTP request function from LuaSocket or do you use the http.request() function that is provided by Defold? I would recommend that you use http.request() from Defold.

I used your extension luasocket.
OK, i will have a look at the defold http.request later.

Edit:
Yey, now it works.

Why does the LuaSocket http.request not work? I wasted much time trying out stuff with that function.

The extension is mine but it’s a primarily a container for the missing parts of LuaSocket. According to the LuaSocket manual an HTTP request should look like this for the simple form:

http.request(url [, body])

or this for the generic form:

http.request{
  url = string,
  [sink = LTN12 sink,]
  [method = string,]
  [headers = header-table,]
  [source = LTN12 source],
  [step = LTN12 pump step,]
  [proxy = string,]
  [redirect = boolean,]
  [create = function]
}

There is as far as I can tell no support for a function callback. The http.request returns multiple values:

“In case of failure, the function returns nil followed by an error message. If successful, the simple form returns the response body as a string, followed by the response status code, the response headers and the response status line. The generic function returns the same information, except the first return value is just the number 1 (the body goes to the sink).”

Since you’re using the generic form the first return value is a 1. You are not specifying a sink to handle the received data.

1 Like

Oh OK. Thanks for your help :slight_smile: