T
T
thisuser2019-09-25 13:27:31
Node.js
thisuser, 2019-09-25 13:27:31

How to output nodejs post request body?

I am using Koa.
There are post requests that come from a third-party service. Send json.
All are perfectly processed, except for one request. For example, this is how I check the content of the request:

router.post('/myevent', async (ctx, next) => {
  let body = ctx.request.body;
  console.log(body); 
  ctx.body = body;
});

Perfectly displays the same sent json to the console.
But one request for some reason displays an empty object `{}`.
The service that sends requests will assure that everything is ok .. I
started looking at the headers that come. Here they are on those requests that are perfectly processed:
{ method: 'POST',
  url: '/myevent',
  header: 
   { host: 'mydomain.ru',
     connection: 'close',
     'content-length': '84584',
     'user-agent': 'GuzzleHttp/6.3.3 curl/7.65.0 PHP/7.3.9',
     'content-type': 'application/json' } }

It is perfectly visible that the content-type is the same json, content-length is excellent. And in the console I see what I need!
And here is the request with an empty object:
{ method: 'POST',
  url: '/myevent',
  header: 
   { host: 'mydomain.ru',
     connection: 'close',
     'content-length': '6335',
     'user-agent': 'GuzzleHttp/6.3.3 curl/7.65.0 PHP/7.3.9' } }

As you can see, there is no `content-type` and `content-length` is much smaller.
From the side of the service, they assure that since the content-length has a certain value, then they send the request perfectly. And I take something clumsily. But how can I process it? All that can be tried, displays an empty object.
Request processing is the same for all, (code above). What can you try?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivandao, 2019-09-25
@thisuser

It's about content-type , say to set it to 'application/json' .
It defaults to text .
Or handle both options - with json and with text

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question