System Information
- Strapi Version: v4 latest
- Operating System: Windows 11
- Database: MySql
- Node Version: 14.16.1
- NPM Version: 7.5.4
- Yarn Version: -
Hi,
I’m trying to create a simple endpoint with a custom controller. In fact, I have multiple but this is all the same issue.
I’m not able to find the proper documentation on how to get the body out of an API call.
Here’s my setup:
- Generate the api struture using CLI strapi generate
- modify the route.js like so:
module.exports = {
routes: [
{
method: 'GET',
path: '/squareup',
handler: 'squareup.index',
},
{
method: 'GET',
path: '/squareup/catalog',
handler: 'squareup.catalog',
}
]
};
- Modify the controller.js like so:
'use strict';
const bggXmlApiClient = require('bgg-xml-api-client');
module.exports = {
async index(ctx, next) {
[... Some code]
body = ctx.request.body;
console.log(body);
},
};
I’m making the call through postman for now using “http://localhost:1337/api/squareup” and a short JSON body.
API is working fine and behave as if there wasn’t any body sent to it. but not being able to send a payload to a call is kind annoying.
I’ve tried a few things that I found while searching but most of it seems to mix up with v3 and not a lot of solution specifically for v4.
- using await next() somewhere in the code before calling body
- config middleware.js for body based on official doc vs v4 solution I found to activate unparsed body.
- import koa unparsed and getting it that way. however, the body now has
“Symbol(unparsedBody):undefined” in it, which I can’t do anything either with.
My middleware.js:
module.exports = [
'strapi::errors',
'strapi::security',
'strapi::cors',
'strapi::poweredBy',
'strapi::logger',
'strapi::query',
{
name: 'strapi::body',
config: {
patchKoa: true,
multipart: true,
includeUnparsed: true,
},
},
'strapi::session',
'strapi::favicon',
'strapi::public',
];
Getting a little confused as to what the real solution is here.
Thanks for the help.