Node JS {"type":"Buffer","data":[]} - node.js

I am making calls to a database through a node JS proxy and sometimes I get back the expected JSON data I am wanting and other times I get {"type":"Buffer","data":[]} as a response. I have never seen this response before and cannot find out why I am getting it sporadically.

For anyone seeing this in the future, my api was restarting when the request was made which resulted in that response. I fixed the issue with the api being down at the time of the request and the response is working properly now.

Related

Node post request body gets truncated

when trying to post a WakeUp event with a JSON body to the Alexa events API using nodejs with axios or request-promise, the API always returns an error 500.
I posted to an online endpoint to actually see what gets posted and learned that the post body gets truncated which obviously results in invalid json. I abstracted the problem and tried to run it from a virgin nodejs installation by using repl.it and the result is the same.
Interestingly enough, there seems to be a relation between the length of the header and the body. So when I shorten the auth token in the header, more characters of the body get transferred. If I shorten the long tokens in the body to about 450 to 500 characters (it seems to vary) the whole request gets through. Obviously this is not a solution, because the tokens are needed for authentication.
When I experimented with the axios version used lowering it to 0.10 I once got a result but posting again lead to another 500. If I post often enough some requests get trough complete, even on the current axios version. I also tried using request-promise with the same outcome.
I got the feeling that I made a really stupid mistake but I can't find it and I really couldn't find anything on this topic, so it's driving me crazy. Any help would be greatly appreciated!
This looks like a tricky one.. first of all, I don't think you're making a really stupid mistake. It looks to me like one of the low-level modules doesn't like something in the POST body for some reason (really weird.).. I've played about with this and I'm getting exactly the same behaviour with both Axios and Request.. if I comment out the tokens (correlationToken and bearer token ) everything works fine.
If I test this locally, everything works as it should (e.g. set up express server and log POST body).
Also posting to https://postman-echo.com/post works as expected (with the original post data)..
I've created this here: https://repl.it/repls/YoungPuzzlingMonad
It looks to me like the original request to http://posthere.io is failing because of the request size only. If you try a very basic POST with a large JSON body you get the same result.
I get the same result with superagent too.. this leads me to believe this is something server side...
This was not related to the post request at all. The reason for the error after sending the WakeUp event was the missing configuration parameter containing the MACAdresses in the Alexa.WakeOnLANController interface.
I used the AlexaResponse class to add the capability via createPayloadEndpointCapability which had not been modified to support the "new" WakeOnLANController interface yet.
It's a pity that the discovery was accepted and my WOL-capable device was added to my smart home devices although a required parameter was missing :(
posthere.io cutting off long post bodys cost me quite a few hours... On the upside, I go to know many different ways of issuing a post request in node ;)
Thanks again Terry for investigating!

Correct Format to send errors in Node Js and Use them in Angular 5

I am practicing Node JS and Angular from past one month. But I am not sure about doing one thing. Whether I should send my errors from my node API using Headers or a JSON Object with the key as an error in my Body...
I have checked some forums, but there is no perfect answer out there. So please spend some time to answer my question guys. Thanks in advance.
Edited: I am using JSON Web Token in my Body part as JSON Format in Login
Well this is quite subjective to answer and will definitely have multiple strategies to work with. This is what I'm doing for my APIs, hope this is useful.
Error messages (JSON) are sent as response body having keys such as "errType" and "errDetail" and maintain this to avoid integration pain. Sometimes, if API is expected to be used by only backend developers, sending some technical error information using key as "errReason".
Send exact response status code like 400, 413 and so on for more clarity.
Not returning any additional header for error reporting.
Let me know your thoughts.

Express.JS net::ERR_CONTENT_LENGTH_MISMATCH for static file

I'm not able to charge one static file over express.js. Sometime, it work, sometime not.
The error i get is net::ERR_CONTENT_LENGTH_MISMATCH
Please understand that i use proxy but the error is occuring too when i was trying the request directly to the server.
So i have to reload the page several time, and sometime, it work

Gmail API keeps returning error 500 Backend Error for a particular email address

For a while it's been working fine. Now today, all of a sudden, when I try to list messages for a particular email address the API call just hangs for a while and then returns Backend Error (500). It still works sometimes, and sometimes it's just really slow at responding, but for the most part it seems to hang.
Any ideas what the problem could be, or how to diagnose it. I'm using the node, googleapis package, oauth2 tokens, server-side calls.
PS: I've checked the logs and there aren't any 4xx errors.

How can I see the whole request and response object in a node.js program?

I have written a web server in nodejs. Most of the time I am receiving a message from one service, doing something, and sending a message to another service. I am in the middle of all the communication.
Sometimes, the communication fails. I am trying to debug what's going on. I would like to examine the request that comes in.
I have a node service, written in express. I have routes, and the routes are passed a req object and a resp object. I should be able to just print out the req object. Problem solved!
But JSON.stringify throws an error. util.inspect doesn't throw an error, but many property values are marked [circular]. The actual property value isn't shown.
When I console.log(req.body) it prints undefined. When I look at req.body using util.inspect, it prints body: {}
I have the feeling the framework is hiding things from me. I don't know how to get the information without it being prettified.
At the tcp/ip level, it's too detailed. At the application level, it's not detailed enough. But at the http level, it should be just right. The request that is received is just text. I should be able to print it out.
I tried using Charles, but I'm having trouble configuring it.
Surely, other people have wanted to see the request as it comes in, before the framework massaged it. How did they do it?
you can use morgan module, it's a HTTP request logger middleware for node.js
I made a more specific question, using a lower level of the node stack of middleware. I got an answer there:
Where did the information I passed in go?
Here is the discussion of how node came to be designed this way:
Node.js - get raw request body using Express
Basically, there used to be a rawBody attribute of the request object in node. People took it out. To accomplish the same thing requires a little bit of code.

Resources