JSON形式で送るとき
https://www.google.co.jp/search?q=wget+post+%E3%83%AA%E3%82%AF%E3%82%A8%E3%82%B9%E3%83%88&ie=utf-8&oe=utf-8&aq=t&hl=ja&gws_rd=ssl#hl=ja&q=curl+json+post+
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"product":{"name":"hoge","quantity":"hoge","location":"hoge"}}' http://localhost:3000/products
画像も含めて送るとき
http://stackoverflow.com/questions/11401553/curl-request-to-upload-image-from-local-machine-to-ruby-on-rails-application
Content-typeにapplication/octet-streamも許可した。
class Product < ActiveRecord::Base has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" } validates_attachment_content_type :photo, :content_type => %w(image/jpeg image/jpg image/png 'image/gif' application/octet-stream) #application/octet-streamを追加 validates_presence_of :name, :quantity, :location end
Protect from forgeryのレベルを変更
class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. #protect_from_forgery with: :exception protect_from_forgery with: :null_session #変更 end
curl -i -v -H 'Content-Type: multipart/form-data' -H 'Accept: application/json' -F "product[name]=hogehoge" -F "product[quantity]=hogehoge" -F "product[location]=hogehoge" -F "product[photo]=@Ghana.png" http://localhost:3000/products * Adding handle: conn: 0x7f892c80aa00 * Adding handle: send: 0 * Adding handle: recv: 0 * Curl_addHandleToPipeline: length: 1 * - Conn 0 (0x7f892c80aa00) send_pipe: 1, recv_pipe: 0 * About to connect() to localhost port 3000 (#0) * Trying ::1... * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 3000 (#0) > POST /products HTTP/1.1 > User-Agent: curl/7.30.0 > Host: localhost:3000 > Accept: application/json > Content-Length: 11138 > Expect: 100-continue > Content-Type: multipart/form-data; boundary=----------------------------295e0abb2f9a > * Done waiting for 100-continue < HTTP/1.1 201 Created HTTP/1.1 201 Created < X-Frame-Options: SAMEORIGIN X-Frame-Options: SAMEORIGIN < X-Xss-Protection: 1; mode=block X-Xss-Protection: 1; mode=block < X-Content-Type-Options: nosniff X-Content-Type-Options: nosniff < Location: http://localhost:3000/products/10 Location: http://localhost:3000/products/10 < Content-Type: application/json; charset=utf-8 Content-Type: application/json; charset=utf-8 < Etag: "42228d0d801806f787ecfec29bdb7c11" Etag: "42228d0d801806f787ecfec29bdb7c11" < Cache-Control: max-age=0, private, must-revalidate Cache-Control: max-age=0, private, must-revalidate < X-Request-Id: 24403851-34f9-4dd6-bd88-e6758669975a X-Request-Id: 24403851-34f9-4dd6-bd88-e6758669975a < X-Runtime: 0.191101 X-Runtime: 0.191101 * Server WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24) is not blacklisted < Server: WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24) Server: WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24) < Date: Thu, 12 Jun 2014 11:50:05 GMT Date: Thu, 12 Jun 2014 11:50:05 GMT < Content-Length: 239 Content-Length: 239 < Connection: Keep-Alive Connection: Keep-Alive < * Connection #0 to host localhost left intact {"id":10,"name":"hogehoge","quantity":"hogehoge","location":"hogehoge","note":null,"photo":"/system/products/photos/000/000/010/original/Ghana.png?1402573805","created_at":"2014-06-12T11:50:05.394Z","updated_at":"2014-06-12T11:50:05.394Z"}