VirtusLab-Open-Source / strapi-plugin-comments

A plugin for Strapi Headless CMS that provides end to end comments feature with their moderation panel, bad words filtering, abuse reporting and more.
MIT License
405 stars 63 forks source link

post method with Axios on Nodejs returns 400 #144

Closed multipliedtwice closed 2 years ago

multipliedtwice commented 2 years ago

Hello, thank you for an awesome plugin. I try to post a new comment from nodejs using Axios:

const result = axios({
    baseURL: 'http://localhost:1337',
    url: `/api/comments/api::blog.blog:${post_id}`,
    method: "POST",
    data: {
      content: 'test',
    }
  }).then(result => {
    console.log('result :>> ', result);
    res.status(200).json(result.data);
  })
    .catch((err) => {
      console.log('err :>> ', err);
      res.status(500).json({ message: err });
    });

But constantly getting an error.

{
  "message": "Request failed with status code 400",
  "name": "Error",
  "stack": "Error: Request failed with status code 400\n    at createError (C:\\Users\\xtate\\rememo\\server\\node_modules\\.pnpm\\axios@0.24.0\\node_modules\\axios\\lib\\core\\createError.js:16:15)\n    at settle (C:\\Users\\xtate\\rememo\\server\\node_modules\\.pnpm\\axios@0.24.0\\node_modules\\axios\\lib\\core\\settle.js:17:12)\n    at IncomingMessage.handleStreamEnd (C:\\Users\\xtate\\rememo\\server\\node_modules\\.pnpm\\axios@0.24.0\\node_modules\\axios\\lib\\adapters\\http.js:293:11)\n    at IncomingMessage.emit (node:events:377:35)\n    at IncomingMessage.emit (node:domain:532:15)\n    at endReadableNT (node:internal/streams/readable:1312:12)\n    at processTicksAndRejections (node:internal/process/task_queues:83:21)",
  "config": {
    "transitional": {
      "silentJSONParsing": true,
      "forcedJSONParsing": true,
      "clarifyTimeoutError": false
    },
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1,
    "maxBodyLength": -1,
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/x-www-form-urlencoded",
      "User-Agent": "axios/0.24.0",
      "Content-Length": 18
    },
    "baseURL": "http://localhost:1337",
    "url": "/api/comments/api::blog.blog:4",
    "method": "post",
    "data": "{\"content\":\"test\"}"
  },
  "status": 400
}

How can I make it work?

cyp3rius commented 2 years ago

Hello @thousandsofraccoons ,

That seems like a problem with Axios itself and encoding the data. I remember there were some issues with this library and especially serialization. Try to stringify the data you're sending. Should help.

cyp3rius commented 2 years ago

Closing as kept without answer.