W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
視頻地址:https://www.bilibili.com/video/BV15R4y1G7hq/
包含以下內容:
GoFrame CLI
?CLI
?創(chuàng)建一個Go項目
視頻地址:https://www.bilibili.com/video/BV1b44y1M7oL/
代碼地址:https://github.com/gogf/gf-demo-user
我們以一個簡單的?API Service
?為例來介紹如何使用?GoFrame
?框架以及相應的?CLI
?工具來開發(fā)一個接口項目。
包含以下內容:
我們通過?curl
?命令來對其中兩個接口執(zhí)行簡單的測試。
用戶注冊 ?- ?/user/signup
??
注冊一個賬號?test001
?,昵稱為?john
?,密碼為?123456
?。
curl -d 'nickname=john&passport=test001&password=123456&password2=123456' http://127.0.0.1:8199/user/sign-up
{"code":0,"message":"","data":null}
我們再次使用剛才的信息注冊一次試試。
curl -d 'nickname=john&passport=test001&password=123456&password2=123456' http://127.0.0.1:8199/user/sign-up
{"code":50,"message":"Passport \"test001\" is already token by others","data":null}
用戶登錄 - ?/user/signin
?
我們先訪問獲取用戶信息的接口,驗證鑒權中間件是否生效。
curl http://127.0.0.1:8199/user/profile
Forbidden
我們用剛才注冊的賬號登錄。
curl -i -d 'passport=test001&password=123456' http://127.0.0.1:8199/user/sign-in
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Origin,Content-Type,Accept,User-Agent,Cookie,Authorization,X-Auth-Token,X-Requested-With
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3628800
Content-Type: application/json
Server: GoFrame HTTP Server
Set-Cookie: gfsessionid=14sc9nep0u6yl0cieluexn0n0w2008q7; Path=/; Expires=Wed, 09 Mar 2022 15:52:44 GMT; SameSite
Trace-Id: 4830f6adbb72da16f34b7162f93080d8
Date: Tue, 08 Mar 2022 15:52:44 GMT
Content-Length: 35
{"code":0,"message":"","data":null}
我們這里使用了?-i
?選項用于終端打印出服務端返回的?Header
?信息,目的是為了獲取?sessionid
?。?GF
?框架默認的?sessionid
?名稱為?gfsessionid
?,我們看到返回的?Header
?中已經(jīng)有了,并且是通過?Cookie
?方式返回的。
隨后我們再次訪問獲取用戶信息接口,并且這次提交?gfsessionid
?,該信息可以通過?Header
?也可以通過?Cookie
?提交,服務端都是能夠自動識別的。
curl -H 'gfsessionid:14sc9nep0u6yl0cieluexn0n0w2008q7' http://127.0.0.1:8199/user/profile
{"code":0,"message":"","data":{"id":1,"passport":"test001","password":"123456","nickname":"john","createAt":"2022-03-08 23:51:40","updateAt":"2022-03-08 23:51:40"}}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: