ga-wdi-boston / game-project

Other
7 stars 102 forks source link

curl request for Game API 'create' not working #593

Closed nriser closed 7 years ago

nriser commented 7 years ago

I'm trying to make my curl request for the create game request, but I keep getting a 404 Not Found response. I've tried two ways, one with POST (as it's written in the game api project repo), and another with GET. The token and ID were retrieved after I signed in. I apologize in advance if I'm missing any other info! I'd appreciate any feedback.

curl request in project-tictactoe/scripts/game-create.sh:

#!/bin/bash
curl --include --request POST http://localhost:4741/games/$ID \
  --header "Authorization: Token token=$TOKEN" \
  --data ''

  echo

error message on grunt server:

Started POST "/games/15" for ::1 at 2017-04-11 19:47:04 -0400

ActionController::RoutingError (No route matches [POST] "/games/15"):
  actionpack (4.2.7.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.2.7.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.7.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.7.1) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.7.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.7.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.7.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.7.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.7.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.7.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  actionpack (4.2.7.1) lib/action_dispatch/middleware/static.rb:120:in `call'
  rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'
  railties (4.2.7.1) lib/rails/engine.rb:518:in `call'
  railties (4.2.7.1) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  puma (3.6.0) lib/puma/configuration.rb:225:in `call'
  puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
  puma (3.6.0) lib/puma/server.rb:415:in `process_client'
  puma (3.6.0) lib/puma/server.rb:275:in `block in run'
  puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'

command I run in terminal, along with the error message below:

$ ID=15 TOKEN=BAhJIiU5NzE5ZTU0ZWZlNjg2NjZlN2JiOWRjYjhlNzM4ODZhNQY6BkVG--41dee1189ccf82dc42516eea6623366857e470f6 sh scripts/game-create.sh
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
X-Request-Id: bc2a179a-da95-4537-a84b-761eca67d271
X-Runtime: 0.021777
Vary: Origin
Content-Length: 1564

<!DOCTYPE html>
<html>
<head>
  <title>The page you were looking for doesn't exist (404)</title>
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <style>
  body {
    background-color: #EFEFEF;
    color: #2E2F30;
    text-align: center;
    font-family: arial, sans-serif;
    margin: 0;
  }

  div.dialog {
    width: 95%;
    max-width: 33em;
    margin: 4em auto 0;
  }

  div.dialog > div {
    border: 1px solid #CCC;
    border-right-color: #999;
    border-left-color: #999;
    border-bottom-color: #BBB;
    border-top: #B00100 solid 4px;
    border-top-left-radius: 9px;
    border-top-right-radius: 9px;
    background-color: white;
    padding: 7px 12% 0;
    box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
  }

  h1 {
    font-size: 100%;
    color: #730E15;
    line-height: 1.5em;
  }

  div.dialog > p {
    margin: 0 0 1em;
    padding: 1em;
    background-color: #F7F7F7;
    border: 1px solid #CCC;
    border-right-color: #999;
    border-left-color: #999;
    border-bottom-color: #999;
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
    border-top-color: #DADADA;
    color: #666;
    box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
  }
  </style>
</head>

<body>
  <!-- This file lives in public/404.html -->
  <div class="dialog">
    <div>
      <h1>The page you were looking for doesn't exist.</h1>
      <p>You may have mistyped the address or the page may have moved.</p>
    </div>
    <p>If you are the application owner check the logs for more information.</p>
  </div>
</body>
</html>

I've also tried it with a GET request instead of a POST:

curl request in project-tictactoe/scripts/game-create.sh:

#!/bin/bash
curl --include --request GET http://localhost:4741/games/$ID \
  --header "Authorization: Token token=$TOKEN" \
  --data ''

  echo

error message on grunt server:

Started GET "/games/15" for ::1 at 2017-04-11 19:31:41 -0400
Processing by GamesController#show as */*
  Parameters: {"id"=>"15"}
  User Load (12.6ms)  SELECT  "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1  [["token", "9719e54efe68666e7bb9dcb8e73886a5"]]
  Game Load (12.4ms)  SELECT  "games".* FROM "games" WHERE (player_x_id = 15 or player_o_id = 15) AND "games"."id" = $1 LIMIT 1  [["id", 15]]
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.24ms)
Completed 404 Not Found in 93ms (Views: 20.5ms | ActiveRecord: 55.3ms)

command I run in terminal, along with the error message below:

$ ID=14 TOKEN=BAhJIiVmODdmMDdjOWM5YWE2MTM0NzRkODNlNmUwMDkyN2U3MwY6BkVG--c5e0c87a931917a82984b92cffa9e1da51ce0570 sh scripts/game-create.sh
HTTP/1.1 404 Not Found
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
X-Request-Id: f0262387-17ce-469c-825e-b59621a04ed1
X-Runtime: 0.044360
Vary: Origin
Transfer-Encoding: chunked

{"error":{"message":"Not Found"}}~/wdi/projects/project-tictactoe (game-api-create)
$ ID=15 TOKEN=BAhJIiU5NzE5ZTU0ZWZlNjg2NjZlN2JiOWRjYjhlNzM4ODZhNQY6BkVG--41dee1189ccf82dc42516eea6623366857e470f6 sh scripts/game-create.sh
HTTP/1.1 404 Not Found
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
X-Request-Id: 50ec95d8-e540-471c-ad1a-7312bd986c2d
X-Runtime: 0.107895
Vary: Origin
Transfer-Encoding: chunked

{"error":{"message":"Not Found"}}~/wdi/projects/project-tictactoe (game-api-create)
nriser commented 7 years ago

I think I may have solved it, but would appreciate it if anyone could confirm this.

I first ran a POST request to make a new game using a sample player x token (player id = 16). This resulted in a Completed 201 response.

  curl --include --request POST http://localhost:4741/games \
    --header "Authorization: Token token=BAhJIiVjYTUwNDkwMGQ5NWRlMGNlMWVjOWVjZjdhZmIzYmEyMwY6BkVG--35d8e6fc70686b4b9cae8f3aa3dd530285a59455" \
    --data ''

echo

response from rails:

Started POST "/games" for ::1 at 2017-04-11 21:29:26 -0400
Processing by GamesController#create as */*
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1  [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
   (0.2ms)  BEGIN
  SQL (14.2ms)  INSERT INTO "games" ("player_x_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["player_x_id", 16], ["created_at", "2017-04-12 01:29:27.052711"], ["updated_at", "2017-04-12 01:29:27.052711"]]
   (12.4ms)  COMMIT
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (33.54ms)
Completed 201 Created in 194ms (Views: 59.7ms | ActiveRecord: 27.4ms)

it seems now that a new game with id=1 has been created with player_x.

(Question: It took me a while to guess that the game id is equal to 1 (because it's the first game I made), but how would I have known that just by reading the rails response?)

Then I ran a GET request to get info on game id=1.

  curl --include --request GET http://localhost:4741/games/1 \
    --header "Authorization: Token token=BAhJIiVjYTUwNDkwMGQ5NWRlMGNlMWVjOWVjZjdhZmIzYmEyMwY6BkVG--35d8e6fc70686b4b9cae8f3aa3dd530285a59455" \
    --data ''

echo

response from rails:

Started GET "/games/1" for ::1 at 2017-04-11 21:41:31 -0400
Processing by GamesController#show as */*
  Parameters: {"id"=>"1"}
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1  [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
  Game Load (1.7ms)  SELECT  "games".* FROM "games" WHERE (player_x_id = 16 or player_o_id = 16) AND "games"."id" = $1 LIMIT 1  [["id", 1]]
[active_model_serializers]   User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 16]]
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (63.27ms)
Completed 200 OK in 109ms (Views: 67.0ms | ActiveRecord: 2.8ms)
jordanallain commented 7 years ago

When creating a new game we don't assign it an ID, that gets handled on the back end. We just want to POST to the /games URL.

nriser commented 7 years ago

@jordanallain Thank you for your reply! I managed to figure that out, but what part of the rails response shows the newly created game id? Is it where it says AND "games"."id" = $1 ?

jordanallain commented 7 years ago

have you logged the response to the console in your success function that runs after creating a game?

nriser commented 7 years ago

Yes, I posted in my second comment. Here is the success response from rails that I get after the POST request:

Started POST # "/games" for ::1 at 2017-04-11 21:29:26 -0400
Processing by GamesController#create as */*
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1  [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
   (0.2ms)  BEGIN
  SQL (14.2ms)  INSERT INTO "games" ("player_x_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["player_x_id", 16], ["created_at", "2017-04-12 01:29:27.052711"], ["updated_at", "2017-04-12 01:29:27.052711"]]
   (12.4ms)  COMMIT
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (33.54ms)
Completed 201 Created in 194ms (Views: 59.7ms | ActiveRecord: 27.4ms)

it seems now that a new game with id=1 has been created with player_x, but I'm wondering if ""/games" for ::1 " in the beginning is where we can confirm that it created a new game with id=1? I knew that it would be game id=1 because it was the first game I created, but how would I know in the future what new game id has just been created just by reading the rails response?

jordanallain commented 7 years ago

no i'm saying in your javascript, in the success function that receives the response from your ajax call, you can console.log(response) or data or whatever you call it to see what is returned from the POST request.

nriser commented 7 years ago

I see what you're saying. This is a curl request though so I haven't done the ajax call yet. Just from testing with the curl script alone, I wanted to make sure that I know what game id was created when I made the POST request. I'm sorry for the lack of clarity.

The response below is the rails response after I did a curl request, not ajax call:

Started POST # "/games" for ::1 at 2017-04-11 21:29:26 -0400
Processing by GamesController#create as */*
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1  [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
   (0.2ms)  BEGIN
  SQL (14.2ms)  INSERT INTO "games" ("player_x_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["player_x_id", 16], ["created_at", "2017-04-12 01:29:27.052711"], ["updated_at", "2017-04-12 01:29:27.052711"]]
   (12.4ms)  COMMIT
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (33.54ms)
Completed 201 Created in 194ms (Views: 59.7ms | ActiveRecord: 27.4ms)
MicFin commented 7 years ago

Review https://github.com/ga-wdi-boston/game-project-api#create

The create action expects a POST with an empty body (e.g '' or '{}' if JSON). If the request is successful, the response will have an HTTP Status of 201 Created, and the body will contain JSON of the created game with player_x set to the user calling create.

And conceptually, why would a game have an ID if it hasn't been created yet? The API says the response will be the game object that contains its ID that was created by the database.

nriser commented 7 years ago

@MicFin Thank you for your response. I understand that ...

{
  "game": {
    "id": 3,
    "cells": ["","","","","","","","",""],
    "over": false,
    "player_x": {
      "id": 1,
      "email": "and@and.com"
    },
    "player_o": null
  }
}

... is the response that we get after we do an ajax call, and that the game ID won't be generated until you create it. I'm not sure if I'm getting my question through properly, but I'm wondering actually only about the curl request and the response I get from rails.

As mentioned in my second comment, I ran a post request from curl script and got a Completed 201 response. My understanding is that at that point, a new game with id=1 has been created with player_x.

Started POST "/games" for ::1 at 2017-04-11 21:29:26 -0400
Processing by GamesController#create as */*
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1  [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
   (0.2ms)  BEGIN
  SQL (14.2ms)  INSERT INTO "games" ("player_x_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["player_x_id", 16], ["created_at", "2017-04-12 01:29:27.052711"], ["updated_at", "2017-04-12 01:29:27.052711"]]
   (12.4ms)  COMMIT
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (33.54ms)
Completed 201 Created in 194ms (Views: 59.7ms | ActiveRecord: 27.4ms)

I was able to confirm it in my curl GET request response,

Started GET "/games/1" for ::1 at 2017-04-11 21:41:31 -0400
Processing by GamesController#show as */*
  Parameters: {"id"=>"1"}
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1  [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
  Game Load (1.7ms)  SELECT  "games".* FROM "games" WHERE (player_x_id = 16 or player_o_id = 16) AND "games"."id" = $1 LIMIT 1  [["id", 1]]
[active_model_serializers]   User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 16]]
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (63.27ms)
Completed 200 OK in 109ms (Views: 67.0ms | ActiveRecord: 2.8ms)

My question is, where in the curl script rails response after the POST request can I see that the game id=1 was created?

jordanallain commented 7 years ago

AND "games"."id" = $1 LIMIT 1 [["id", 1]] <--

nriser commented 7 years ago

@jordanallain Great, that makes sense. thank you!

@MicFin Thank you for chatting with me.

My issue was that I was checking the rails response only, and not the response I was getting in my working terminal. I see that the game ID is explicitly written in the response! I apologize for the confusion.

$ TOKEN="BAhJIiVlY2ZkYzIxOTRkNzQ1NzlmNTNlYTA2YmI4MDY1YzZhNwY6BkVG--6538aee6891e5b6873ea220139ecb7aeebc2ec13" sh scripts/game-create.sh
HTTP/1.1 201 Created
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
X-Request-Id: 9ab43b59-3b39-4395-bcba-07f1fba7768d
X-Runtime: 0.062126
Vary: Origin
Transfer-Encoding: chunked

{"game":{"id":6,"cells":["","","","","","","","",""],"over":false,"player_x":{"id":13,"email":"ee"},"player_o":null}}