简体   繁体   中英

node.js - KOA server- Foward augmented POST request

So I was looking for a while now for a simple solution for this but couldn't find anything clear.

My goal is to receive HTTP Post request from html form to my KOA server and forward it to remote API.

As you can understand from the question, I'm complete beginer for not being able to do this, but my code so far looks like this:

var koaBody = require('koa-body')()
publicRouter.post('/file', koaBody,
    function *(next) {
      var post = this.request.body
      console.log(post)
      // augment post

    }
)

Currently I'm able to receive file to the server and I do want to learn what should I add in comment line (I assume there) in order to augment post request with additional data, such as keys, signatures and content-type details.

So first of all, how should I create this augmented POST?

And how to forward it? I assume for that I can use promises (Q.denodeify(require('request'))), like I have been able to do it with GET request

Use co-request ( here ) to send make remote API calls.

var request = require('co-request');
var koaBody = require('koa-body')();
publicRouter.post('/file', koaBody,
    function *(next) {
      var post = this.request.body
      console.log(post)
      yield request({
         url: '/some/remote/api',
         method: 'POST',
         body: body
      });
    }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM