I am writing node.js App. I am able to get data entered from postman via "x-www-form-urlencoded". But When I enter data with "form-data",the data is not accepted in API.I have tried with app.use(bodyParser.urlencoded({extended:false})) and app.use(bodyParser.urlencoded({extended:true})) . But still it is not working. Is there any way I can accept data via "form-data"?
As suggested in post you cannot use body-parser for form-data.
Instead, you can use a module like multer.
Related
I am using the example given here: https://github.com/expressjs/multer. However instead of using the form in the example, I am using Postman to POST some data, however I am getting an empty body. I have set Postman to use form-data. Any ideas what the problem could be?
Thanks
I have written auth routes in Nestjs and wanted to use it with the form-data. I got it working with URL-encoded-form-data, JSON, text, but not receiving anything in the body when I use form-data and really want it to work with form-data as on the front-end I am hitting the route with form-data. I have tried every way I could find on the web, but none of them helped me in this case. so after hours of searching and trying when I didn't get any lead I am posting my question here.
Any Kind of Help is appreciated.
Code of signup endpoint
Main.ts configurations
empty body I am getting on postman
NestJS provides a built-in multipart/form-data parser which you can access using a FileInterceptor.
Here's an example of how you'd read a single file (as you've shown in your screenshot):
#Post('signup')
#UseInterceptors(FileInterceptor('<name of file here - asdasd in your screenshot>'))
signup(#UploadedFile() file, #Body() body) {
console.log(file);
console.log(body);
}
I want to POST the data from body to mongodb but when i use CORS then i am not able to post the data , GET method in working. What configuration we have to make in CORS middleware
This error and resolution came up as the first result of my google query using your error message and expressjs Cannot POST / error using express
I've implemented a webhook for a facebook chatbot with php using laravel and all works fine, when I message my bot I receive a post request with expected data structure and I manage to have all working well. Then I was trying to do the webhook implementation using nodejs but when I message my bot the post request that I receive is not the one it would be expected. This is kind of weird because I was able to validate the webhook with the token. I have used the same facebook app and page that I used for the php implementation so I don't think the problem is there. Here's the code in node: http://pastebin.com/0GQcXdV2
I would expect the request structure to be: http://pastebin.com/GFU89LjA
but instead it's this: http://pastebin.com/51S7DrkG
I'm sorry if this question seems stupid and I'm missing something obvious but can't figure out what. I'm kind of new to node js so maybe this is a newbie mistake, but if anyone can tell me what am I doing wrong it would be very appreciated. Thanks in advance
I managed to solve my problem by importing npm body-parser and make my express app use it for returning JSON. According to the npm documentation the bodyParser object provides middleware factories that expose the body of the request and assign it to req.body in plain text, json , raw or url encoding form body (https://www.npmjs.com/package/body-parser). To solve my problem i just added the following two lines of code:
var bodyParser = require('body-parser');
app.use(bodyParser.json());
More information on body parser can be found here.
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.