hollyw
(Holly W)
2020 年 8 月 10 日午後 3:59
1
「公開ユーザー一覧」エンドポイントでテストを行っていたところ、残りのユーザーを取得するためにページネーションが必要でした。次のページのパスを取得するために load_more_directory_items フィールドを使用しましたが、そのパスは /directory_items?order=days_visited&page=1&period=all としてリストされていました。
しかし、このパスを使用すると、ユーザーを含む適切な API 応答が返ってこず、代わりに HTML が返ってきました。元のエンドポイントと比較すると、directory_items の後に .json が欠落しているように見えます。次のページのパスに .json を追加すると、正常に動作しました。これは API のバグでしょうか?
「いいね!」 1
blake
(Blake Erickson)
2020 年 8 月 11 日午前 12:30
2
おそらく、404 Not Found の HTML ページが表示されたのでしょう。
API を使用する際は、すべての API リクエストで必ず .json を付けてください。多くのエンドポイントはデフォルトで JSON を返しますが、一部は HTML を返すためです。今回のルートにはデフォルトが設定されていないようです。そのため、.json が指定されなかった場合、ルートが見つからなかったのです。
このエラーページを防ぐためにも、.json を明示的に指定すべきですが、API 呼び出しを検知した場合、読めない HTML ボディを 200 で返すのではなく、実際の 404 HTTP レスポンスを返すべきではないかと考えています ?
「いいね!」 5
hollyw
(Holly W)
2020 年 8 月 11 日午後 2:19
4
はい、初期の API 呼び出しで指定したエンドポイントには .json を含めていますが、問題となったのはレスポンス内の meta 以下にある load_more_directory_items に .json が指定されていなかった点です。
"meta": {
"last_updated_at": "2020-08-11T11:40:43.000Z",
"total_rows_directory_items": 1132,
"load_more_directory_items": "/directory_items?order=days_visited&page=2&period=all"
}
現在の値は:/directory_items?order=days_visited&page=2&period=all
本来あるべき値は:/directory_items.json?order=days_visited&page=2&period=all
.json が含まれていないため、ページネーションを行うたびにこの文字列を手動で修正する必要があります。
「いいね!」 1
blake
(Blake Erickson)
2020 年 8 月 11 日午後 4:02
5
ああ、今おっしゃっている意味がわかりました。それはすぐに修正できるはずです。今日中に作業します。
「いいね!」 2
blake
(Blake Erickson)
2020 年 8 月 11 日午後 7:56
6
この問題に対する修正をプッシュしました:
committed 06:43PM - 11 Aug 20 UTC
The `/directory_items` route needs to have a .json url, but the rails
url helper… `_path` doesn't return the format of the route.
I tried passing in a format options to `directory_items_path`. Which
works in the rails console
```
[8] pry(main)> directory_items_path(params.merge(:format => :json))
=> "/directory_items.json?page=1"
```
but when I added that some logic to the controller it comes out as
```
/directory_items?format=json&page=1
```
(which is actually how I expect it to work based on how you pass in the
format param). Anyways, because I couldn't figure out how to pass a
format to the `_path` helper I just used URI.parse to append `.json`
manually.
git pull を実行して再ビルドすれば、あなたのインスタンスでも利用可能になります。ご報告いただきありがとうございます 。
「いいね!」 3