title: Grape API service response 400 bad request at Rails 4 when I sending a json request to it using curl
body: <p>I don't know how to solve the 400 bad request that API service gives back to me. All I need are receiving json responses from API service, and the API service could communicate with many-type devices. <p>
  I'm building an all-json-request API service according to tutorial of Grape gem at GitHub.com <a href="https://github.com/ruby-grape/grape" rel="nofollow">https://github.com/ruby-grape/grape</a> and GitHub.io <a href="http://intridea.github.io/grape/docs/index.html" rel="nofollow">http://intridea.github.io/grape/docs/index.html</a> . After I sending a json request to the API service: localhost:3000/specified_vegetables using curl, which is construct with <strong>Grape gem version 0.8.0</strong> and located at <strong>new empty Rails 4.1.1 project</strong>, the API service give back the response:</p>
  
  <pre><code>*   Trying ::1...
  * connect to ::1 port 3000 failed: Connection refused
  *   Trying 127.0.0.1...
  * Connected to localhost (127.0.0.1) port 3000 (#0)
  &gt; GET /specified_vegetables/ HTTP/1.1
  &gt; Host: localhost:3000
  &gt; User-Agent: curl/7.43.0
  &gt; Accept: application/json
  &gt; Content-Type:application/json
  &gt; Content-Length: 55
  &gt;
  * upload completely sent off: 55 out of 55 bytes
  &lt; HTTP/1.1 400 Bad Request
  &lt; Content-Type: application/json
  &lt; Content-Length: 56
  &lt; Cache-Control: no-cache
  &lt; X-Request-Id: 87231177-b8f8-4084-b0ef-19445fa42fab
  &lt; X-Runtime: 0.220101
  &lt; Server: WEBrick/1.3.1 (Ruby/2.2.1/2015-02-26)
  &lt; Date: Thu, 01 Oct 2015 13:35:46 GMT
  &lt; Connection: Keep-Alive
  &lt;
  * Connection #0 to host localhost left intact
  {"error":"transaction_date is missing, name is missing"}
  </code></pre>
  
  <p><br>
  My curl request command: <code>curl -H 'Accept: application/json' -H 'Content-Type:application/json' -X GET http://localhost:3000/specified_vegetables/ -d '{"transaction_date" : "20131031", "name" : "金針筍"}' -v</code>
  In my database, there is one record matched the conditions I've queried.
  <br>
  <br>
  <p>
  The log of rails server runs and it also includes the curl request:</p>
  
  <pre><code>=&gt; Booting WEBrick
  =&gt; Rails 4.1.1 application starting in development on http://0.0.0.0:3000
  =&gt; Run `rails server -h` for more startup options
  =&gt; Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
  =&gt; Ctrl-C to shutdown server
  [2015-10-01 21:35:43] INFO  WEBrick 1.3.1
  [2015-10-01 21:35:43] INFO  ruby 2.2.1 (2015-02-26) [x86_64-darwin10]
  [2015-10-01 21:35:43] INFO  WEBrick::HTTPServer#start: pid=27004 port=3000
  
  
  Started GET "/specified_vegetables/" for 127.0.0.1 at 2015-10-01 21:35:46 +0800
  </code></pre>
  
  <p><br>
  <p>
  The following are my environment settings and codes.<br>
  The directory structure of API files in rails app:</p>
  
  <blockquote>
    <p>RailsApp.root/app/api/agriculture_transaction/base.rb
    RailsApp.root/app/api/agriculture_transaction/v1/specified_vegetables.rb</p>
  </blockquote>
  
  <p><br>
  Code of <strong>base.rb</strong>:</p>
  
  <pre><code>module AgricultureTransaction
          class Base &lt; Grape::API
                  format :json
                  version 'v1'
  
                  mount AgricultureTransaction::V1::SpecifiedVegetables
                  mount AgricultureTransaction::V1::OverviewVegetables
          end
  end
  </code></pre>
  
  <p><br>
  Code of <strong>specified_vegetables.rb</strong></p>
  
  <pre><code>module AgricultureTransaction
         module V1
                  class SpecifiedVegetables &lt; Grape::API
                          format :json
                          default_format :json
                          version 'v1', using: :header, vendor: 'twitter'
  
                          resource :specified_vegetables do
                                  desc "Get all transaction prices of all items today."
                                  get :today do
                                          SpecifiedVegetable.where(transaction_date: Date.today).find_in_batches do |vegetables|
                                                  vegetables
                                          end
                                  end
  
                                  desc "Get transaction prices of delegated item in a delegated day."
                                  params do
                                          requires :transaction_date, type: Date, desc: 'code'
                                          requires :name, type: String
                                  end
                                  get do
                                          SpecifiedVegetable.where(transaction_date: params[:transaction_date], name: params[:name]).find_in_batches do |vegetables|
                                                  vegetables
                                          end
                                  end
                          end
                  end
         end
  end
  </code></pre>
  
  <p><br>
  The only one model in Rails app is <strong>SpecifiedVegetable.rb</strong>:</p>
  
  <pre><code>class SpecifiedVegetable &lt; ActiveRecord::Base
          self.table_name = "specified_vegetable"
  end
  </code></pre>
  
  <p><p>
  In <strong>config/application.rb</strong> file, I add two lines in rails <strong>Application class</strong>:</p>
  
  <pre><code>config.paths.add "app/api", glob: "**/*.rb"
  config.autoload_paths += Dir["#{Rails.root}/app/api/*"]
  </code></pre>
  
  <p><p>
  In <strong>config/routes.rb</strong> file, I add one line in Rails.application.routes.draw:</p>
  
  <pre><code>mount AgricultureTransaction::Base =&gt; '/'
  </code></pre>
  
  <p>Above of all are information I could provide. Please help me to solve this problem or give me some keywords to search. I've find some articles, but they could not help me.</p>