-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
## How to use | ||
First terminal | ||
> go run credit_card.go 8080 | ||
Second terminal | ||
> curl -X POST http://localhost:8080/ -d '{ "number": "4003600000000014" }' | ||
received result: {"valid":true} | ||
|
||
## What learned | ||
### Create a http server | ||
> http.HandleFunc("/", creditCardValidator) | ||
- First parameter "/" is the location of url | ||
- If this parmater is /test, then the request address is http://localhost:8080/test | ||
- Second parameter is a function that deal the http request. | ||
|
||
> http.ListenAndServe(":"+port, nil) | ||
Set the listen address and start the http server | ||
|
||
### Http handle function | ||
> func funcNmae(writer http.ResponseWriter, request *http.Request) | ||
- Get the request body | ||
> json.NewDecoder(request.Body).Decode((&cardNumber)) | ||
- Check the request type | ||
> if request.Method != http.MethodPost {} | ||
- Write response | ||
- Response with error | ||
> http.Error(writer, "Invalid request method", http.StatusMethodNotAllowed) | ||
- Reponse with json data | ||
> writer.Header().Set("Content-Type", "application/json") | ||
writer.Write(jsonResponse) |