04_update-user-controllers-get

command
v0.0.0-...-6ea799e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2022 License: MIT Imports: 4 Imported by: 0

README

In this step:

We will get a user from mongodb

First we will get the user id from the URL

id := p.ByName("id")

Next we will Verify that the id is an ObjectId

if !bson.IsObjectIdHex(id) {
	w.WriteHeader(http.StatusNotFound) // 404
	return
}

ObjectIdHex returns an ObjectId from the provided hex representation.

	oid := bson.ObjectIdHex(id)

Now we will create an empty user to store the results in

	u := models.User{}

And then we will get the user information

if err := uc.session.DB("go-web-dev-db").C("users").FindId(oid).One(&u); err != nil {
	w.WriteHeader(404)
	return
}

Run this code

  1. Start your server

POST a user to mongodb

Enter this at the terminal

curl -X POST -H "Content-Type: application/json" -d '{"name":"James Bond","gender":"male","age":32}' http://localhost:8080/user

-X is short for --request Specifies a custom request method to use when communicating with the HTTP server.

-H is short for --header

-d is short for --data

GET a user from mongodb

Enter this at the terminal

curl http://localhost:8080/user/<enter-user-id-here>

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL