why is my data undefined when i make an http request? - node.js

I have a problem with my backend when I send my data to the API req.body sends me null {}
suddenly I don't understand why I can't recover my data, however on the front side I send the data well ?

if you're using form data then include url encoded parser like this::
app.use(express.urlencoded({extended : true}));
app.use(express.json());
add this in your main server file where you create your server

It may be the order of variables, haven't tried, but that is the only thing that sticks out. When you are sending its {email,name,password2,password} but when you are receiving it its {name,email,password,password2}
Edit: messed up variable orders when replying ironically

Related

AWS lambda api gateway with node.js express returns an error of "net::ERR_CONTENT_DECODING_FAILED 200"

The error occurs when my client sends a GET request to the node.js server hosted with AWS lambda api gateway. The server is expected to send back an array of objects with res.json({}). The weird part is that when i test with a response of object or an array of objects with lesser variables in it, it works. I have also tried to JSON.stringify() the array in the server side and JSON.parse() in the client but to no avail. Thanks in advance to everyone helping me and do guide me along as its my first time posting on StackOverflow.
Edit: However i have tried it with a curl command to the endpoint and it returns the json array without any errors.
Alright everyone i found which part of my code caused the bug!
app.use(busboy());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(busboyBodyParser());
As i was playing around with file transfer before with the npm package 'Busboy', imported and used some packages associated with it. However, i forgotten to comment out these codes and thus, i believe it messed up the response body and got the error. Just to be clear this was in the node.js backend.

node.js server: HTTP POST body is empty

I am sending info from a wi-fi connected arduino device (cc3000) to an AWS server running express/node. I plan to eventually hook the endpoint up to SQS as a producer, but for now I am just trying to get a POST request to work. The problem is, I am on secure shell on my AWS and the body portion is empty as I see post requests stream in. However, the headers are present as I have sent them. My question is, where is the data going? Is this a formatting problem or something wrong with AWS?
Raw body of POST request (I am just using plain/text for now to see if I can get this to work):
POST /postdata/ HTTP/1.1
Host: *******************
User-Agent: Arduino/0.6.0
Accept: plain/text
Content-Length: 36
Content-Type: plain/text
{"temperature_c": "29.80"}
Again, the headers are in the JSON payload, but not the "temperature" portion.
Here is the endpoint:
app.post('/postdata', function (req, res) {
Sensor_data.create(req.body);
console.log(req.body);
});
The headers are present. This is intended to write to a mongoDB. If I do a cURL request from the command line, it writes successfully. For example, this works:
curl -v *************** -d "{temp_f=28.40&relative_humidity=45.40"
So if the headers are there, I know I am successfully writing something to the server. Where the heck is the data? Is this a formatting problem or something wrong with AWS?
## EDIT ########:
I double checked to make sure body-parser was set up. I had installed the middleware in my server.js as so:
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
However, another issue has arisen. I have some code in this route to help me see what is going on by logging to console. I changed the POST request to application/json and know it is parsing because I had a small typo that I had to fix. However, once it started parsing correctly, nothing logged to console and nothing wrote to mongoDB. Any idea what is happening there? If the JSON is in fact being parsed, where is it being received on the server?
This issue was solved by setting app.use(bodyParser.urlencoded({ extended: false })); to be initialized with true and creating the post request with Accept and Content-Type to application/x-www-form-urlencoded. Hope this post helps some people as I have have seen quite a few posts with people struggling with this issue
It isn't clear whether you've set up the appropriate middleware to do the parsing. You'll find documentation here - in the first example you'd need the json parser, and in the second you'd need urlencoded (although there appears to be an extra leading { - a typo, perhaps?).
I am just using plain/text for now to see if I can get this to work
That's not going to work. The json-parsing middleware decides whether to try to parse based on the content-type - if you aren't honest, it doesn't know it should try to parse. Use application/json if you're going to send a JSON body.

Fetch post data after a request in NodeJS

i' m a bit new to Node, so question may be stupid...
I am sending a POST request to a website (through http.request) and I want to be able to use the invisible POST data I get along the response.
I hope this is achievable, and I think so since I am able to preview those data in Chrome debugger.
PS : I understand that we can use BodyParser to parse and get those while listening for a POST call server side, but I have found no example of how to use it coupled with an http.request.
Thanks !
If the body of the HTTP response contains JSON, then you need to parse it first in order to turn it from a string into a JavaScript object, like this:
var obj = JSON.parse(body);
console.log(obj.response.auth_token);
More info on various ways of sending a POST request to a server can be found here: How to make an HTTP POST request in node.js?
Edit : So we figured it out in the comments. The value I needed was in the form to begin with, as a hidden field. My error was to think it was generated afterward. So I'll just grab it first then login, so I can use it again for future POST requests on the website.

node.js body on http request object vs body on express request object

I'm trying to build an http module that suppose to work with an express server.
while reading the http module api, I see that it doesn't save the body inside the request object.
So my questions are:
If I want to build an express server which works with the official http module, how should I get the body?
I consider to implement the http module in the following way: listening to the socket, and if I get content-length header, listetning to the rest of the socket stream till I get all the body, save it as a memeber of the http request, and only then send the request object to the express server handler.
What are the pros and cons of my suggestion above vs letting the express server to "listen" to the body of the request via request.on('data',callback(data))
I mean , why shouldn't I keep the body inside the 'request' object the same way I keep the headers?
It's hard to answer your question without knowing exactly what you want to do. But I can give you some detail about how the request body is handled by Node/Express, and hopefully you can take things from there.
When handling a request (either directly via Node's request handler, or through Express's request handlers), the body of the request won't automatically be received: you have to open an HTTP stream to receive it.
The type of the body content should be determined by the Content-Type request header. The two most common body types are application/x-www-form-urlencoded and multipart/form-data. It's possible, however, to use any content type you want, which is usually more common for APIs (for example, using application/json is becoming more common for REST APIs).
application/x-www-form-urlencoded is pretty straightforward; name=value pairs are URL encoded (using JavaScript's built-in encodeURIComponent, for example), then combined with an ampersand (&). They're usually UTF-8 encoded, but that can also be specified in Content-Type.
multipart/form-data is more complicated, and can also typically be quite large, as vkurchatkin's answer points out (meaning you may not want to bring it into memory).
Express makes available some middleware to automatically handle the various types of body parsing. Usually, people simply use bodyParser, though you have to be careful with that middleware. It's really just a convenience middleware that combines json, urlencoded, and multipart. However, multipart has been deprecated. Express is still bundling Connect 2.12, which still includes multipart. When Express updates its dependency, though, the situation will change.
As I write this, bodyParser, json, urlencoded, and multipart have all been removed from Connect. Everything but multipart has been moved into the module body-parser (https://github.com/expressjs/body-parser). If you need multipart support, I recommend Busboy (https://npmjs.org/package/busboy), which is very robust. At some point, Express will update it's dependency on Connect, and will most likely add a dependency to body-parser since it has been removed from Connect.
So, since bodyParser bundles deprecated code (multipart), I recommend explicitly linking in only json and urlencoded (and you could even omit json if you're not accepting any JSON-encoded bodies):
app.use(express.json());
app.use(express.urlencoded());
If you're writing middleware, you probably don't want to automatically link in json and urlencoded (much less Busboy); that would break the modular nature of Express. However, you should specify in your documentation that your middleware requires the req.body object to be available (and fail gracefully if it isn't): you can go on to say that json, urlencoded, and Busboy all provide the req.body object, depending on what kind of content types you need to accept.
If you dig into the source code for urlencoded or json, you will find that they rely on another Node module, raw-body, which simply opens the request stream and retrieves the body content. If you really need to know the details of retrieving the body from a request, you will find everything you need in the source code for that module (https://github.com/stream-utils/raw-body/blob/master/index.js).
I know that's a lot of detail, but they're important details!
You can do that, it fairly simple. bodyParser middleware does that, for example (https://github.com/expressjs/body-parser/blob/master/index.js#L27). The thing is, request body can be really large (file upload, for example), so you generally don't want to put that in memory. Rather you can stream it to disk or s3 or whatnot.

How to close a request in nodejs?

What I have:
I'm using node.js with the express framework and I'm building a form where I can upload files, I'm using node-formidable for this.
What's the problem
My problem is that I can't close the request if I found an error with the uploaded files. I want to check file types, size etc.. So I can get the proper files uploaded, and the files are actually not uploaded so I don't waste time.
So the problem is that I can't stop the HTTP request.
What I have tried
Here is what I have tried so far:
request.connection.destroy();
response.end('something went wrong...');
I assume that the connection.destroy() aborts the request, and I know this because it fires the formidables form abort event (form.on('abort', function(){ ... })) But the file is still uploading, and the response doesn't arrives just after the file was uploaded.
So how should I close the HTTP Request, and send a message back to the client?
EDIT:
And something else, when I don't use response.end() then it works, except that the Client waits for the the answer, it's weird :\
The correct way for doing this is:
request.pause();
response.status = 400;
response.end('something went wrong...');
Source: https://groups.google.com/forum/?fromgroups=#!topic/nodejs/2gIsWPj43lI
Try with following code:
When your server side validation fails, give a response like:
res.json({'IsFileUploaded':'false', 'requiredFileSize': '1000bytes', 'actualFileSize': '800bytes', 'AlertMsg':'File size should be at least 1000 bytes....'})
It closes connection as well as you can pass your data in JSON format

Resources