Skip to content

Instantly share code, notes, and snippets.

@khanhhd
Last active December 25, 2015 08:29
Show Gist options
  • Save khanhhd/6946767 to your computer and use it in GitHub Desktop.
Save khanhhd/6946767 to your computer and use it in GitHub Desktop.

Ví dụ sử dụng lệnh cURL

## 1. Tạo một user ```sh $ curl http://localhost:3000/users/new --cookie-jar cookie.txt ``` get được màn hình new, ở form, chú ý thẻ thẻ này sẽ được dùng ở option data trong câu lệnh bên dưới với name và value của nó. ```sh $ curl http://localhost:3000/users --request POST --data "user[name]=khanh" –data "user[email]=framgia@gmail.com" --data "user[password]=123456" --data "user[password_confirmation]=123456" --data-urlencode "authenticity_token=YbDFZIDQKHZlOZv4qUu+QJV8B1zp8IIvpmmLKWTLwtw=" --cookie cookie.txt ``` Kết quả như sau ( trên máy local của tôi) ```html You are being redirected. ``` ##2. Đăng nhập với user vừa tạo vào trang sign in ```sh $ curl http://localhost:3000/signin --cookie cookie.txt ``` Post data lên ```sh $ curl http://localhost:3000/sessions --request POST --data "session[email]=framgia@gmail.com" --data "session[password]=123456" --data-urlencode "authenticity_token=YbDFZIDQKHZlOZv4qUu+QJV8B1zp8IIvpmmLKWTLwtw=" --cookie cookie.txt ``` Kết quả: ```html You are being redirected. ``` ##3. Sign out ```sh $curl http://localhost:3000/signout --request DELETE --data-urlencode "authenticity_token=ktNWSasl44SDAHM/Dzr84N0jzxAbibiJRYhymq1WU4k=" --cookie "cookie.txt" --cookie-jar "cookie.txt" ``` ##4. Show index of post ```sh $ curl http://localhost:3000/users/1 ``` ##5 . Tạo một post

Đến trang chứa form post

$ curl http://localhost:3000 --cookie "cookie.txt" --cookie-jar "cookie.txt"

Nhập dữ liệu vào form

$ curl http://localhost:3000/microposts --request POST --data-urlencode "authenticity_token=KjV/+P0SnleUkHt54s6oiKbkpUiDSJe6KUgWEWOXukk=" --data "micropost[content]=something" --cookie "cookie.txt" --cookie-jar "cookie.txt

Sau khi lệnh chạy thành công trong db sẽ có dữ liệu của post vừa được tạo:

User.find_by(email: “framgia@gmail.com “).microposts.last

SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? ORDER BY updated_at ASC LIMIT 1  [["user_id", 109]]
 => #<Micropost id: 311, content: "something", user_id: 109, created_at: "2013-10-12 05:37:24", updated_at: "2013-10-12 05:37:24"> 

##6. Del một post Giả sử bạn có một micropost id là 312 sao khi đăng nhập bạn thể delete nó:

$ curl http://localhost:3000/microposts/312 --request DELETE --data-urlencode "authenticity_token=ktNWSasl44SDAHM/Dzr84N0jzxAbibiJRYhymq1WU4k=" --cookie cookie.txt --cookie-jar "cookie.txt"

##7. Follow Sau khi đăng nhập với một user nào đó và giả sử bạn có một user với id = 109 Show user 109

$ curl http://localhost:3000/users/109 --cookie-jar "cookie.txt" --cookie "cookie.txt"

Sau đó chạy lệnh

$ curl http://localhost:3000/relationships --request POST --data "relationship[followed_id]=109" --data-urlencode "authenticity_token=ktNWSasl44SDAHM/Dzr84N0jzxAbibiJRYhymq1WU4k=" --cookie cookie.txt --cookie-jar "cookie.txt"

##8. Unfollow

$ curl http://localhost:3000/relationships/102 --request DELETE --data-urlencode "authenticity_token=ktNWSasl44SDAHM/Dzr84N0jzxAbibiJRYhymq1WU4k=" --cookie cookie.txt --cookie-jar "cookie.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment