Getting ctx body in v4 on a GET call

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. :stuck_out_tongue:

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.

After a lot of search, try and error and so on, I finally got what I wanted through the koa-body reference in the doc.

However, there is not a single example on how to configure the middleware anywhere so that was hard to figure out since every example out there, including yours on github are v3.

Should update that a litle.

And the answer to my issue was to add “parsedMethods : [‘GET’, ‘POST’, ‘PUT’, ‘PATCH’],” to my “strapi::body” configuration above. GET doesn’t have it’s body parsed by default in koa.

4 Likes

Also getting a similar error. Putting your parsedMethods solution didn’t work. Added the "includeUnparsed: true," to the strapi::body and imported const unparsed = require("koa-body/unparsed.js"); as well as trying the Symbol method recommended on koa-body, but whenever I try to access the raw body const unparsedBody = ctx.request.body[unparsed]; I just get undefined. Can anyone from Strapi help with this? It feels like there’s something going on with how the middlewares are being handled or setup by Strapi that’s preventing the raw body from being accessed, as otherwise my settings are the exact same as all the examples

2 Likes

That’s it! Thank you!

All i needed to do was to ad the parseMethods to the middleware like:

module.exports = [
“strapi::errors”,
“strapi::security”,
“strapi::cors”,
“strapi::poweredBy”,
“strapi::logger”,
“strapi::query”,
{
name: “strapi::body”,
config: {
parsedMethods: [“GET”, “POST”, “PUT”, “PATCH”],
},
},
“strapi::session”,
“strapi::favicon”,
“strapi::public”,
];

Then ctx.request.body is not undefined anymore.
Strapi 4.13.6