牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

"POST JSON data to simple rails application with curl" をやってみたメモ

fix: 20120826

Create a Project

$ rails new blog
:
$ cd blog

Create a Scaffold

$ rails generate scaffold entry title content
:

Migrate

$ rake db:migrate
:

CURL 001

$ curl -d "entry[title]=title01" -d "entry[content]=content01" http://localhost:3000/entries
<html><body>You are being <a href="http://localhost:3000/entries/1">redirected</a>.</body></html>

Check


Server

WARNING...

Started POST "/entries" for 127.0.0.1 at 2012-08-26 18:53:28 +0900
Processing by EntriesController#create as */*
  Parameters: {"entry"=>{"title"=>"title01", "content"=>"content01"}}
WARNING: Can't verify CSRF token authenticity
   (0.1ms)  begin transaction
  SQL (0.7ms)  INSERT INTO "entries" ("content", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?)  [["content", "content01"], ["created_at", Sun, 26 Aug 2012 09:53:28 UTC +00:00], ["title", "title01"], ["updated_at", Sun, 26 Aug 2012 09:53:28 UTC +00:00]]
   (2.1ms)  commit transaction
Redirected to http://localhost:3000/entries/1
Completed 302 Found in 18ms (ActiveRecord: 2.9ms)

CURL 002

$ curl -v -H "Accept: application/json" -H "Content-type: application/json" -d '{"entry": {"title": "title02", "content":"content02"}}' http://localhost:3000/entries
* About to connect() to localhost port 3000 (#0)
*   Trying 127.0.0.1...
* connected
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /entries HTTP/1.1
> User-Agent: curl/7.25.0 (x86_64-apple-darwin11.3.0) libcurl/7.25.0 OpenSSL/1.0.1c zlib/1.2.7 libidn/1.22
> Host: localhost:3000
> Accept: application/json
> Content-type: application/json
> Content-Length: 54
> 
* upload completely sent off: 54 out of 54 bytes
< HTTP/1.1 201 Created 
< Location: http://localhost:3000/entries/2
< Content-Type: application/json; charset=utf-8
< X-Ua-Compatible: IE=Edge
< Etag: "ed62147237458401398a4b39ab9126a6"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: 10a869568a12bbb44007d62233231c24
< X-Runtime: 0.008603
< Content-Length: 120
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
< Date: Sun, 26 Aug 2012 09:55:26 GMT
< Connection: Keep-Alive
< Set-Cookie: _blog_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJWVhMTMxODcyYzhkYjU5ZjBhODZlMmI3ZWQ5M2UwOTA0BjsAVA%3D%3D--a31c86aff70a1673603304c3c57c28426a3dfe8f; path=/; HttpOnly
< 
* Connection #0 to host localhost left intact
{"content":"content02","created_at":"2012-08-26T09:55:26Z","id":2,"title":"title02","updated_at":"2012-08-26T09:55:26Z"}* Closing connection #0

Check


server
Started POST "/entries" for 127.0.0.1 at 2012-08-26 18:55:26 +0900
Processing by EntriesController#create as JSON
  Parameters: {"entry"=>{"title"=>"title02", "content"=>"content02"}}
WARNING: Can't verify CSRF token authenticity
   (0.1ms)  begin transaction
  SQL (0.6ms)  INSERT INTO "entries" ("content", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?)  [["content", "content02"], ["created_at", Sun, 26 Aug 2012 09:55:26 UTC +00:00], ["title", "title02"], ["updated_at", Sun, 26 Aug 2012 09:55:26 UTC +00:00]]
   (1.5ms)  commit transaction
Completed 201 Created in 5ms (Views: 1.0ms | ActiveRecord: 2.2ms)