I have
http://localhost:8080/?key=ahFkZXZ-ZGV2LWVkdW5hdGlvbnIOCxIIVXNlckluZm8YLAw
I would like to ask on how to:
- Decode and convert the "key" to a
*datastore.Key
- And use it to get an entity.
Thanks for your help!
I have
http://localhost:8080/?key=ahFkZXZ-ZGV2LWVkdW5hdGlvbnIOCxIIVXNlckluZm8YLAw
I would like to ask on how to:
*datastore.Key
Thanks for your help!
First: You should think about which packages you need this case. Since you're trying to read a GET
value from a URL
you need probably a function from net/http
.
In particular: FormValue(key string)
returns GET
and POST
parameters.
Second: Now open the appengine/datastore
documentation and find functions which do the following:
string
to a *datastore.Key
(DecodeKey(encoded string))Now it's a really easy thing:
func home(w http.Response, r *http.Request) {
c := appengine.NewContext(r)
// Get the key from the URL
keyURL := r.FormValue("key")
// Decode the key
key, err := datastore.DecodeKey(keyURL)
if err != nil { // Couldn't decode the key
// Do some error handling
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Get the key and load it into "data"
var data Data
err = datastore.Get(c, key, data)
if err != nil { // Couldn't find the entity
// Do some error handling
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}