johnbrett / Getting-Started-with-hapi.js

Code examples for the book: Getting Started with hapi.js
https://gettingstartedwithhapijs.com
27 stars 16 forks source link

Example 3 - User Store Example #7

Closed johncrosby closed 8 years ago

johncrosby commented 8 years ago

I seem to have it running but how do I actually Create a user and Retrieve a user?

johnbrett commented 8 years ago

Hi @johncrosby. Have you used a REST Client or CURL before? If you POST a request to 127.0.0.1:1337/user it will create a user. To retrieve a user, you would then make a GET request to 127.0.0.1:1337/user/{USER_ID} where the user ID would be found in the response of the POST request.

johncrosby commented 8 years ago

I havent used REST client or CURL before. Is CURL an instruction that I would need to type into a new Terminal tab or can It be done from the browser address bar? Good book btw 👍

johnbrett commented 8 years ago

Yep, CURL can be run from the command line (from most operating systems, I'm not sure about windows).

A quick intro to CURL, to POST you can use the following: curl -H "Content-Type: application/json" -X POST -d '{"username":"john", "nationality": "Irish"}' 127.0.0.1:1337/user

where:

To retrieve a user is then: curl 127.0.0.1:1337/user/d115de8e-5cb4-4f08-8c42-9d9151cb120f

A GET request is always easier :) You could actually paste that url into your browser and it would return the user to you.

Here's an example of me creating a user and retrieving them via CURL:

2__bash

If you have any more questions let me know, and thanks for the feedback on the book! Really appreciate it! :)

johncrosby commented 8 years ago

Thank you thats very helpful.