Content-Type = application/json; utf-8 #4150
-
Good afternoon. I'm implementing a payment by pix, where the Bank has a service that needs a route to notify if payment has been made. I need some data that arrives in the body of this request. The problem is that they Content-Type = application/json; utf-8, this Content-Type is causing request.body() to return an empty object. Could anyone help with this problem? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Can you share a screenshot of the request you are making? Along with headers and the request body? |
Beta Was this translation helpful? Give feedback.
-
Copy-pasting it from my Discord response Won't accept, coz it's invalid header Standard itself: https://datatracker.ietf.org/doc/html/rfc4627 I'd send off email to bank to fix it on their side import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
export default class Customjson {
public async handle({request, }: HttpContextContract, next: () => Promise<void>) {
// Check for non-standard JSON content-type header
if (request.header('content-type') === 'application/json; utf-8') {
let rawBuffer = await request.request.read()
let rawBodyString = rawBuffer.toString('utf-8')
let parsedBody = JSON.parse(rawBodyString)
request.setInitialBody(parsedBody)
}
await next()
}
} That works as a middleware to use |
Beta Was this translation helpful? Give feedback.
-
Thanks for helping resolve the issue. It could have this treatment directly in AdonisJS. |
Beta Was this translation helpful? Give feedback.
-
It could have support, as I had this problem in the integration of BRADESCO's PIX API. That according to them application/json, utf8 is not standard. Their application is developed by IBM. I got to test the integration with AspNet.Core and I had no problem accessing the Body's content, but as my application was built on top of AdonisJs, I was looking for a solution so I didn't have to create a microservice with another language/Framework. |
Beta Was this translation helpful? Give feedback.
Copy-pasting it from my Discord response
Won't accept, coz it's invalid header
According to standard, only acceptable header is
application/json
without anything extraBut also it's quite widespread to use
application/json; charset=utf-8
charset
needs to be defined in most APIs if you want it to be parsed all goodJust slapping
utf-8
in the end doesn't follow even any recommendationsAccording to standard it can be
utf-8
,utf-16
orutf-32
and it should be detected by analyzing first 4 bytes of incoming data, not based on content type headerStandard itself: https://datatracker.ietf.org/doc/html/rfc4627
I'd send off email to bank to fix it on their side
Meanwhile, I'll see if can figure ou…