Skip to content

Instantly share code, notes, and snippets.

@electricimp
Last active August 29, 2015 14:13
Show Gist options
  • Save electricimp/464393f1c2e10f38eb86 to your computer and use it in GitHub Desktop.
Save electricimp/464393f1c2e10f38eb86 to your computer and use it in GitHub Desktop.
Sample code for httpresponse.headers() usage with 'set-cookie'
http.onrequest(function(request, response){
// This issues cookies with path=/, which means that the browser
// sends them to any page on agent.electricimp.com;
// this means that other agents can read it.
local url = http.agenturl()
// url is https://agent.electricimp.com/agentID, so use 'split' to
// generate an array of sub-strings separated by /
local urlParts = split(url, "/")
// The agentID is the third element in the array
local pathField = "path=/" + urlParts[2]
// Add the pathField to your cookie header
response.header("Set-Cookie", "cookie=123456789 ; max-age=3600 ; " + pathField)
// Issue the response to the browser
response.send(200, "OK")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment