Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
Express Body Parser with Custom Header

I am in the middle of setting up a REST API that takes requests with a custom Content-Type, but I am having trouble parsing the body with the NPM package body-parser. I am running some tests with Mocha and Chai-HTTP and seding my request tests like this:

chai.request(server)
.post('/demo')
.set('Content-Type', 'application/vnd+companyName.v01+json')
.send({name: 'test'})
.end(function(err, res) {/* tests are here */});

In my express app's app.js, I am calling this middleware:

app.use(bodyParser.json({type: 'application/*+json'}));

When I make the type more general, like making it 'application/*', I can pass the request through with a 'application/json' Content-Type, but not my custom one. When I do this, my req.body is an empty object. If bodyParser was just completely not working, req.body would be undefined and not an empty object. By looking at the docs, I feel like my options on my bodyParser call are correct, but clearly not - any insight?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

vnd+companyName.v01+json isn't a valid media type.

A valid media type should look like:

[ tree. ] subtype name [ +suffix ] [ ; parameters ]

The subtype name can't contain . or + characters, those are reserved for the (optional) tree and suffix, respectively (RFC6838).

So in your case, the mime type should look like this:

application/vnd.companyName-v01+json

However, it seems that there's an additional requirement imposed by body-parser (or rather, type-is, which is used to match content type), in that the subtype name needs to be lower case:

application/vnd.companyname-v01+json

Strangely, that requirement only applies for the body-parser configuration part, the client is allowed to use upper case in its requests.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda