I've built a rails API that receives JSON. I want to validate that the request body is valid JSON and provide a valid error on the API response if not. Spent quite a lot of time trying different options and trying to find an answer but no success.
Whatever I do to try and catch the error, it always throws the following error when I send some incorrect test JSON.
JSON::ParserError at /api/v1/apikey123
743: unexpected token at '{
"query": "hi there" (missing comma here on purpose)
"lang": "en",
"sessionId": "en" }
json (2.0.2) lib/json/common.rb, line 156
``` ruby
151 # additions even if a matching class and create_id was found. This option
152 # defaults to false.
153 # * *object_class*: Defaults to Hash
154 # * *array_class*: Defaults to Array
155 def parse(source, opts = {})
> 156 Parser.new(source, opts).parse
157 end
158
159 # Parse the JSON document _source_ into a Ruby data structure and return it.
160 # The bang version of the parse method defaults to the more dangerous values
161 # for the _opts_ hash, so be sure only to parse trusted _source_ documents.
Here is my code:
module Api
class ApiController < ApplicationController
protect_from_forgery with: :null_session
before_action :authenticate, :parse_request
private
def parse_request
begin
@user_input = JSON.parse(request.body.read)
rescue JSON::ParserError => e
return false
end
end
...
end
end
I'd like to know how to handle this without throwing an error and send back a response that has an error message with "Invalid JSON format"