Axios post request.body - empty object

I am trying to post data from my reaction. Backend - Express. Here is the database code:

var express = require('express'); var app = express(); var bodyParser = require("body-parser"); var methodOverride = require("method-override"); var mongoose = require("mongoose"); var expressSanitizer = require("express-sanitizer"); mongoose.connect("mongodb://localhost/blog-react"); //app config app.set("view engine", "ejs"); app.use(express.static("public")); app.use(bodyParser.urlencoded({extended: true})); //must be after parser app.use(expressSanitizer()); app.use(methodOverride("_method")); //schema config var blogSchema = new mongoose.Schema({ title: String, image: String, body: String, //it should be date. With default value now. created: { type: Date, default: Date.now } }); var Blog = mongoose.model("Blog", blogSchema); function handle500(response, error){ console.log(error.stack); response.status(500); response.json({error: "error: internal server error"}); } app.post("/api/blogs", function(request, response){ var blog = { title: request.sanitize(request.body.title), image: request.sanitize(request.body.image), body: request.sanitize(request.body.body) }; console.log(request.body); Blog.create(blog, function(error, newBlog){ if(error){ console.log("inside post handler ERROR") handle500(response, error); } else{ console.log("inside post handler OK") response.json({status: "success"}); } }); }); 

Change code:

  var requestUrl = "/api/blogs"; var blog = { title: "a", image: "b", body: "c" } axios.post(requestUrl, blog) .then(function(response){ console.log("success",response.data) }) .catch(function(response){ console.log("error", response); }); 

When I send data through axios - request.body is always {} But if I send data through the usual form - that's right - request.body contains all the expected data.

What am I doing wrong with the axioms?

+7
source share
2 answers

You are missing one middleware, bodyParser.json() . Add it to your configuration.

 mongoose.connect("mongodb://localhost/blog-react"); app.set("view engine", "ejs"); app.use(express.static("public")); app.use(bodyParser.json()); // <--- Here app.use(bodyParser.urlencoded({extended: true})); 
+17
source

It looks like you only have two points left for this to work:

First, the http method must be set to POST instead of GET, since you want to send something.

two: then you can add the http header (as you did with the authorization header) Content-Type: 'application / json'

In the end, be sure to use some package of the body parser utilities, such as this: body-parser, and install it in your application.

I assume your server is using express, here is how you do it with express:

 const express = require('express'); const app = express(); const bodyParser = require('body-parser') const jsonParser = bodyParser.json(); app.use(jsonParser); // use it globally app.get('your_route', jsonParser, otherMiddleware, (req, res) => ...); // use it for specific routes /* ... rest of your code */ 
0
source

Source: https://habr.com/ru/post/1260495/


All Articles