環境
macOS Monterey 12.3.1
ruby 3.1.2
rails 6.1.6
エラー内容
Talend API TesterでRails APIのテストを実施していたところ、以下のエラーが発生した。
Talend API Tester
{
"status": 400,
"error": "Bad Request",
"exception": "#<ActionDispatch::Http::Parameters::ParseError: 859: unexpected token at '{\n
\"name\": \"team1\",\n}\n'>",
...
}
発生時の状況
POST "http://localhost:3000/api/v1/teams" で以下の情報を送信していた。
{
"name": "team1",
}
コントローラは以下の様に設定していた。
teams_controller.rb
class Api::V1::TeamsController < ApplicationController
def create
team = Team.new(team_params)
if team.save
render json: { team: }, status: :created
else
render json: {}, status: :internal_server_error
end
end
end
対処方法
こちらの記事を参考に、JSONLint に送信したjsonをコピペしてチェックしました。
末尾に不要なコンマが入っていたため、エラーが発生していました。
修正後、無事リクエストが通るようになりました。