Express + Postman, req.body is empty

前端 未结 4 1870
醉梦人生
醉梦人生 2020-12-05 05:47

I know this has been asked multiple times, but I have been looking around and still can\'t find an answer to my problem.

Here is my code, I make sure to use and conf

相关标签:
4条回答
  • 2020-12-05 05:57

    In my case, I solve it By Adding "type":"text" to urlencoded item int the exported collection json file generated by postman. I observe it because there are some of my request is successfully done. The difference is the missing type field in the json generated postman collection file. This problem also happen to my teammate.

    before (failed request): "body": { "mode": "urlencoded", "urlencoded": [ { "key": "email", "value": "{{userEmail}}" }, { "key": "password", "value": "{{userPassword}}" } ] }

    after (successful request): "body": { "mode": "urlencoded", "urlencoded": [ { "key": "email", "value": "{{userEmail}}", "type": "text" }, { "key": "password", "value": "{{userPassword}}", "type": "text" } ] }

    I also write parser script in javascript language to handle it.

    const fs = require('fs');
    let object = require(process.argv[2]);
    
    function parse(obj) {
        if(typeof obj === 'string') return;
        for(let key in obj) {
            if(obj.hasOwnProperty(key)) {
                if(key === 'urlencoded') {
                    let body = obj[key];
                    for(let i = 0;i < body.length;i++) {
                        body[i].type = "text";
                    }
                }
                else parse(obj[key]);
            }
        }
    }
    
    parse(object);
    fs.writeFile('ParsedCollection.json', JSON.stringify(object, null, '\t'), function(err){
        //console.log(err);
    });
    

    Just run it in terminal node parser.js <json postman collection file path> and it will output in ParsedCollection.json file. After that import this file into postman.

    0 讨论(0)
  • 2020-12-05 06:02

    AFAIK you need to use the Body-Parser : https://github.com/expressjs/body-parser

    bodyParser = require('body-parser').json();
    app.post('/itemSearch', bodyParser, function(req, res) {
      //var Keywords = req.body.Keywords;
      console.log("Yoooooo");
      console.log(req.headers);
      console.log(req.body);
      res.status(200).send("yay");
    });
    

    Then try with PostMan setting the body as raw json:

    {
      "test": "yay"
    }
    
    0 讨论(0)
  • 2020-12-05 06:07

    Try this

     // create application/json parser
        app.use(bodyParser.json());
        // parse various different custom JSON types as JSON
        app.use(bodyParser.json({ type: 'application/*+json' }));
        // parse some custom thing into a Buffer
        app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }));
        // parse an HTML body into a string
        app.use(bodyParser.text({ type: 'text/html' }));
        // parse an text body into a string
        app.use(bodyParser.text({ type: 'text/plain' }));
        // create application/x-www-form-urlencoded parser
        app.use(bodyParser.urlencoded({ extended: false }));
    
    0 讨论(0)
  • 2020-12-05 06:13

    After spending a few hours I realized need to change the Raw type in postman to JSON

    0 讨论(0)
提交回复
热议问题