0

Posting data to an endpoint which is working fine when I select raw and send json.

I am not receiving anything when I select form-data. I have already defined them in my server.js.

server.js config:

const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
var corsOptions = {
  origin: "http://localhost:8081"
};

app.use(cors(corsOptions));
app.use(express.static('app/uploaded'));
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

Here is the postman request.

enter image description here

Ali Asgher Badshah
  • 811
  • 1
  • 11
  • 34
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • 1
    are you sending a POST request? it says PATCH there – d9ngle Jun 16 '20 at 14:30
  • Does this answer your question? [Postman Chrome: What is the difference between form-data, x-www-form-urlencoded and raw](https://stackoverflow.com/questions/26723467/postman-chrome-what-is-the-difference-between-form-data-x-www-form-urlencoded) I think you want x-form-urlencoded. – O. Jones Jun 16 '20 at 14:34
  • @d9ngle my bad, updated it – Danyal Sandeelo Jun 16 '20 at 14:35
  • @O.Jones x-form-urlencoded is working fine. I will be sending files over this request so I need to test using form-data – Danyal Sandeelo Jun 16 '20 at 15:22

1 Answers1

3

hi for parsing different form of data you have to add different middleware to access it

here is the code for parsing response:

var express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();
var app = express();

// for parsing application/json
app.use(bodyParser.json()); 

// for parsing application/xwww-
app.use(bodyParser.urlencoded({ extended: true })); 
//form-urlencoded

// for parsing multipart/form-data
app.use(upload.array()); 

refer this doc for more detail documentation of body-parser with form data

let me know if its help

Ali Asgher Badshah
  • 811
  • 1
  • 11
  • 34