Hi everyone, I’m developing an API endpoint that receives forms params and a file in the same request and save it using Carrierwave. When I test it through a HTML form or through an Insomnia request, I get the following error:
JSON::GeneratorError at /[path]
source sequence is illegal/malformed utf-8
Ruby |
/home/uanderson/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.7/lib/active_support/core_ext/object/json.rb : in to_json , line 40 |
---|---|
Web | POST 127.0.0.1/[path] |
Looking at the stack trace, it appears that the error originates from an attempt to log the request:
/home/uanderson/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/jets-3.1.1/lib/jets/controller/base.rb: in json_dump
display_event["queryStringParameters"] = parameter_filter.filter(display_event['queryStringParameters'])
display_event["pathParameters"] = parameter_filter.filter(display_event['pathParameters'])
json_dump(display_event)
end
# Handles binary data safely
def json_dump(data)
JSON.dump(data) ### <= HERE
rescue Encoding::UndefinedConversionError
data['body'] = '[BINARY]'
JSON.dump(data)
end
def controller_paths
paths = []
/home/uanderson/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/jets-3.1.1/lib/jets/controller/base.rb: in
event_log
display_event['body'] = '[BASE64_ENCODED]'
else
display_event['body'] = parameter_filter.filter_json(display_event['body'])
end
display_event["queryStringParameters"] = parameter_filter.filter(display_event['queryStringParameters'])
display_event["pathParameters"] = parameter_filter.filter(display_event['pathParameters'])
json_dump(display_event) ### <= HERE
end
# Handles binary data safely
def json_dump(data)
JSON.dump(data)
rescue Encoding::UndefinedConversionError
data['body'] = '[BINARY]'
/home/uanderson/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/jets-3.1.1/lib/jets/controller/base.rb: in log_start
# Documented interface method, careful not to rename
def log_start
# JSON.dump makes logging look pretty in CloudWatch logs because it keeps it on 1 line
ip = request.ip
Jets.logger.info "Started #{@event['httpMethod']} \"#{@event['path']}\" for #{ip} at #{Time.now}"
Jets.logger.info processing_log
Jets.logger.info " Event: #{event_log}" ### <= HERE
The curl request:
curl --request POST \
--url http://127.0.0.1:8888/[path] \
--header 'Authorization: Bearer ey...' \
--header 'Content-Type: multipart/form-data' \
--form 'status[user]=email@email.com' \
--form 'status[date]=2023-09-26' \
--form 'status[value]=600' \
--form 'status[file]=@C:\Users\Uanderson\Pictures\file1.png' \
--form 'status[uuid]=1'
Has anyone encountered this error before?