1

I am unable to make a POST method request with . I am unable to send a request object with it.

For example see this example. Here only GET request is being made. But I want to make a POST request.

Temp O'rary
  • 5,366
  • 13
  • 49
  • 109

2 Answers2

0

If you want to send a POST request to an another host it would be easier to use some libraries dedicated to this task like axios or request.

DBalazs
  • 133
  • 2
  • 9
0

I was facing the same issue. When I tried to make a POST request I was receiving undefined as the value of the body object.

I ended up not using koa-http-request as the example provided in your link. I have used both koa-bodyparser and koa-router middlewares instead:

const Koa = require('koa');

const KoaRouter = require('koa-router');
const bodyParser = require('koa-bodyparser');

const app = new Koa();
const router = new KoaRouter();

// =====  M i d d l e w a r e s  =====
// Body parser 
app.use(bodyParser());
// Router
app.use(router.routes()).use(router.allowedMethods());

// ...

const addItem = async (context) => {
  const body = await context.request.body;

  // Now you can access the body object
  console.log(body)

}

// ...

router.post('/', addItem);

In my case it had worked as it supposed to. :)