如何将json数据发布到Action创建?

时间:2016-08-01 07:17:11

标签: ruby-on-rails json arduino

  # POST /players
  # POST /players.json
  def create
    #  player.create(params[:player])
    @player = Player.new(player_params)

    respond_to do |format|
      if @player.save
        format.html { redirect_to @player, notice: 'Player was successfully created.' }
        format.json { render :show, status: :created, location: @player }
      else
        format.html { render :new }
        format.json { render json: @player.errors, status: :unprocessable_entity }
      end
    end
  end

我的播放器只有一个数据名称。我想我可以将一个json数据发布到/tickets.json来创建一个新模型。是不是? 那怎么办呢?

POST http://www.address.com/tickets.json HTTP/1.1 
Content-Type: application/json;charset=utf-8

{"name":"test"}

http协议中的代码是否正确?

抱歉我的英文。

我使用arduino mkr1000发布json 这是我的arduino代码:

    client.println("POST  http://192.168.1.11:3000/players.json HTTP/1.1");
    client.println("Content-Type: application/json");
    client.println("Accept: application/json");
    client.println("{\"name\":\"test\"}");

rails服务器返回:

2016-08-01 16:56:14 +0800: HTTP parse error, malformed request (): #<Puma::HttpParserError: Invalid HTTP format, parsing fails.>
2016-08-01 16:56:14 +0800: ENV: {"rack.version"=>[1, 3], "rack.errors"=>#<IO:<STDERR>>, "rack.multithread"=>true, "rack.multiprocess"=>false, "rack.run_once"=>false, "SCRIPT_NAME"=>"", "QUERY_STRING"=>"", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"puma 3.6.0 Sleepy Sunday Serenity", "GATEWAY_INTERFACE"=>"CGI/1.2", "REQUEST_METHOD"=>"POST"}
---

0 个答案:

没有答案