IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

RealWorld Example を触ってみる

Haskell Servant RealWorld Example App

リポジトリの場所

Haskell Servant RealWorld Example App

インストー

サイトの手順の通り

データベース

>sqlite3 /tmp/haskell-servant-test.db <  {your_path}/haskell-servant-realworld-example-app/scripts/schema.sql 

処理

登録

xxx$ curl --verbose -H "Content-Type: application/json" -X POST -d '{"user" : {"email":"aaa@a.com", "username":"aaa", "password": "aaa"} }' http://127.0.0.1:8081/api/users
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> POST /api/users HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 70
>
* upload completely sent off: 70 out of 70 bytes
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Sat, 03 Jun 2017 02:32:50 GMT
< Server: Warp/3.2.11.2
< Content-Type: application/json
<
* Connection #0 to host 127.0.0.1 left intact
{"user":{"bio":null,"email":"aaa@a.com","image":null,"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuI","username":"aaa"}}

認証

成功

xxx$ curl --verbose -H "Content-Type: application/json" -X POST -d '{"user" : {"email":"aaa@a.com", "username":"aaa", "password": "aaa"} }' http://127.0.0.1:8081/api/users/login
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> POST /api/users/login HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 70
>
* upload completely sent off: 70 out of 70 bytes
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Sat, 03 Jun 2017 02:39:33 GMT
< Server: Warp/3.2.11.2
< Content-Type: application/json
<
* Connection #0 to host 127.0.0.1 left intact
{"user":{"bio":null,"email":"aaa@a.com","image":null,"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuI","username":"aaa"}}

失敗

xxx$ curl --verbose -H "Content-Type: application/json" -X POST -d '{"user" : {"email":"aaa@a.com", "username":"aaa", "password": "xxx"} }' http://127.0.0.1:8081/api/users/login
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> POST /api/users/login HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 70
>
* upload completely sent off: 70 out of 70 bytes
< HTTP/1.1 401 Unauthorized
< Transfer-Encoding: chunked
< Date: Sat, 03 Jun 2017 02:39:42 GMT
< Server: Warp/3.2.11.2
<
* Connection #0 to host 127.0.0.1 left intact
Incorrect login or password

ユーザ取得

成功

xxx$ curl --verbose -H "Authorization: Token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuI"  http://127.0.0.1:8081/api/user
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> GET /api/user HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.43.0
> Accept: */*
> Authorization: Token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuI
>
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Sat, 03 Jun 2017 02:28:27 GMT
< Server: Warp/3.2.11.2
< Content-Type: application/json
<
* Connection #0 to host 127.0.0.1 left intact
{"user":{"bio":null,"email":"aaa@a.com","image":null,"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuI","username":"aaa"}}

失敗

xxx$ curl --verbose -H "Authorization: Token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuX"  http://127.0.0.1:8081/api/user
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> GET /api/user HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.43.0
> Accept: */*
> Authorization: Token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuX
>
< HTTP/1.1 401 Unauthorized
< Transfer-Encoding: chunked
< Date: Sat, 03 Jun 2017 02:37:18 GMT
< Server: Warp/3.2.11.2
<
* Connection #0 to host 127.0.0.1 left intact
Wrong 'Authorization' token

プロファイル取得

成功

xxx$ curl --verbose -H "Authorization: yJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuI" -X GET  http://127.0.0.1:8081/api/profiles/aaa
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> GET /api/profiles/aaa HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.43.0
> Accept: */*
> Authorization: yJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuI
>
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Sat, 03 Jun 2017 02:41:39 GMT
< Server: Warp/3.2.11.2
< Content-Type: application/json
<
* Connection #0 to host 127.0.0.1 left intact
{"profile":{"bio":null,"image":null,"username":"aaa","following":false}}

失敗

xxx$ curl --verbose -H "Authorization: yJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuI" -X GET  http://127.0.0.1:8081/api/profiles/xxx
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> GET /api/profiles/xxx HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.43.0
> Accept: */*
> Authorization: yJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuI
>
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Sat, 03 Jun 2017 02:42:11 GMT
< Server: Warp/3.2.11.2
< Content-Type: application/json
<
* Connection #0 to host 127.0.0.1 left intact
{"profile":null}
xxx$

記事作成

xxx$ curl --verbose -H "Content-Type: application/json" -H "Authorization: Token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuI" -X POST -d '{"article": {"title" : "aaa_title_1", "description" : "aaa_description_1", "body" : "aaa_body_1", "tag" : "aaa_tag_1"} }' http://127.0.0.1:8081/api/articles
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> POST /api/articles HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Authorization: Token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6eyJ1c2VyIjoiYWFhIn19.2_OPvQfqSdJfqftP-4ovSzv6zLFcyaMHPK6XdV1NuuI
> Content-Length: 120
>
* upload completely sent off: 120 out of 120 bytes
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Sat, 03 Jun 2017 02:47:30 GMT
< Server: Warp/3.2.11.2
< Content-Type: application/json
<
* Connection #0 to host 127.0.0.1 left intact
{"article":{"createdAt":"2017-06-03T02:47:30Z","slug":"aaa_title_1","body":"aaa_body_1","favorited":0,"tagList":null,"favoritesCount":0,"author":{"bio":null,"image":null,"username":"aaa","following":false},"updatedAt":"2017-06-03T02:47:30Z","title":"aaa_title_1","description":"aaa_description_1"}}
xxx$

記事一覧

xxx$ curl --verbose -X GET  http://127.0.0.1:8081/api/articles
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> GET /api/articles HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Sat, 03 Jun 2017 02:50:09 GMT
< Server: Warp/3.2.11.2
< Content-Type: application/json
<
* Connection #0 to host 127.0.0.1 left intact
{"articlesCount":1,"articles":[{"createdAt":"2017-06-03T02:47:30Z","slug":"aaa_title_1","body":"aaa_body_1","favorited":0,"tagList":null,"favoritesCount":0,"author":{"bio":null,"image":null,"username":"aaa","following":false},"updatedAt":"2017-06-03T02:47:30Z","title":"aaa_title_1","description":"aaa_description_1"}]}

記事取得

成功

root)blender:servant03 ogawanaoto$ curl --verbose -X GET  http://127.0.0.1:8081/api/articles/aaa_title_1
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> GET /api/articles/aaa_title_1 HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Sat, 03 Jun 2017 02:54:31 GMT
< Server: Warp/3.2.11.2
< Content-Type: application/json
<
* Connection #0 to host 127.0.0.1 left intact
{"article":{"createdAt":"2017-06-03T02:47:30Z","slug":"aaa_title_1","body":"aaa_body_1","favorited":0,"tagList":null,"favoritesCount":0,"author":{"bio":null,"image":null,"username":"aaa","following":false},"updatedAt":"2017-06-03T02:47:30Z","title":"aaa_title_1","description":"aaa_description_1"}}

失敗

xxx$ curl --verbose -X GET  http://127.0.0.1:8081/api/articles/aaa_title_x
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> GET /api/articles/aaa_title_x HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Sat, 03 Jun 2017 02:54:35 GMT
< Server: Warp/3.2.11.2
< Content-Type: application/json
<
* Connection #0 to host 127.0.0.1 left intact
{"article":null}