0

enter image description here

can somebody help me to understand why I get an empty body?

on the documentation says that express.json() uses body-parser so it is the same thing I use express.json() or bodyParser() ?

on documentation also says I can provide the type of the header via the options settings on json object options... i think its like this:

app.use(express.json({ type: 'application/json' }))

my headers content type says: 'content-type': 'application/x-www-form-urlencoded' tho the default is "application/json", as the docs says.

I need to get the form value back as a json so i can save on database and stuff.

docs i am refering to: http://expressjs.com/en/api.html#express.json

nishi
  • 512
  • 1
  • 4
  • 19
  • 1
    Possible duplicate https://stackoverflow.com/a/42129247/1231844 – OFRBG Aug 24 '20 at 20:32
  • 1
    express.json() is unable to parse form fields the way that you want by default. You will need to change your type to 'application/x-www-form-urlencoded' – Chris Aug 24 '20 at 20:36
  • ok so if i set app.use(express.json()) as a middleware and configure it to accept x-www-form-urlencoded, it will work on a route, but what happens if on another route like app.post('/users/avatar'...) i wanna a image, or in app.get('/users/me'...) i want a json... how can i configure these type of things? @Cehhiro thanks for showing the link but i think its a lil bit different. – nishi Aug 24 '20 at 20:56
  • i tried this: app.use(express.urlencoded({ extended: true })) app.use(express.json({ type: 'application/x-www-form-urlencoded' })) still getting empty object. tho when i use postman it does work. – nishi Aug 24 '20 at 21:40

1 Answers1

0

alright, i figured that out.

the whole problem is with the html input. not with the parser configurations.

the problem is that when you are sending something via postman for example, if you wanna send application/x-www-form-urlencoded type, postman will give you a interface for you to set a key value pair. if yo usend a raw json tahts the same thing as "application/json" i think, you will have to set a valid json, with key value pairs.

on postman, if you send either without providing a key to the value, the req.body will result being empty, as express says in the docs

"A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body), or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred."

i never understood the reason of "name" attribute on html inputs and stuff. well, i humbly think that this is the value (i havent searched yet).

the solution was to set a name attribute on the input =>

the response of the body was => name:'... whatever was passed on the input ...'

code:

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>

    <body>
        <form action="/" , method="POST">
            <!-- <input type="text" id="name" name="name" placeholder="name" required> -->
            <input type="text" name="name"> // name attribute is the key "name" for your input.
            <input type="text" name="age"> // name attribute is the key "age" for your attribute.
            <input type="submit">
        </form>
    </body>

</html>

javascript:

const express = require('express')
const path = require('path')

const app = express()

app.use(express.urlencoded({ extended: true }))
app.use(express.json())


app.get('/', (req, res) => {
    res.sendFile('index.html', { root: path.join(__dirname) })
})

app.post('/', (req, res) => {
    console.log(req.body) // the result is the input value.
})

app.listen(3000, () => {
    console.log('working')
})
nishi
  • 512
  • 1
  • 4
  • 19