How do I query form data using koa?
I am trying to replicate the behavior of a login form via koa.
Login form:
<form id="loginForm" method="post" action="http://myaddress:3000/auth" enctype="multipart/form-data">
I am using koa request and form-data modules:
var form = new FormData();
form.append('identification', 'userId');
form.append('password', 'userPassword');
var options = {
url: DB_SERVER_URL + 'auth',
method: 'POST',
formData: form
};
var response = yield request(options);
console.log('response.statusCode: ' + response.statusCode);
But I always get a 400 response.
I've tried using only form.submit(DB_SERVER_URL + 'auth', function(err, res) { ... }
that works, but I love the koa yield functionality, and ideally I want to avoid having to handle callbacks.
Any ideas?
source to share
Koa accepts multiple output inputs, which can be derived from your current code more or less easily depending on your current setup:
-
Promise. Since the form data doesn't use it, we'll create one with Q
var Q = require('q'); var promise = Q.ninvoke(form, "submit", DB_SERVER_URL + 'auth'); var response = yield promise; console.log('response.statusCode: ' + response.statusCode);
-
or thunk is a wrapper function, as you used in your answer, but there are libraries that can handle you for wrapping ( thunkify-wrap here ):
var thunkify = require('thunkify-wrap'); var submit = thunkify(form.submit, form); // the context is needed in this case var response = yield submit(DB_SERVER_URL + 'auth'); console.log('response.statusCode: ' + response.statusCode);
source to share
I ended up using it form.submit(DB_SERVER_URL + 'auth', function(err, res) { ... }
, but wrapped the callbacks to use yield
to keep control flow synchronized.
Here's my wrapper for the callback form.submit
to get the response:
function makeLoginRequest(formData) {
var form = new FormData();
form.append('identification', formData.identification);
form.append('password', formData.password);
var DB_SERVER_URL = 'http://myurl:3000/';
return function(callback) {
form.submit(DB_SERVER_URL + 'auth', function(error, response) {
callback(error, response);
});
}
}
And here is my cover for the callback to get the response body:
function getLoginResponseData(response) {
return function(callback) {
response.on("data", function(chunk) {
callback(null, chunk);
});
}
}
This allows me to use yield
to maintain a synchronous flow of control:
var response = yield makeLoginRequest(this.request.body);
console.log('response.statusCode: ' + response.statusCode);
var chunk = yield getLoginResponseData(response);
console.log("BODY: " + chunk);
I'm a node and koa newbie, so if you have a better way, let me know!
source to share
If you are using koa request I was able to do it.
const request = require('koa-request');
const response = yield request({
method: 'POST',
url: 'https://whatsever.com',
form: {
itema: 'vala',
itemb: 'valb',
},
headers: {
'Content-type': 'application/x-www-form-urlencoded'
}
});
this.body = response.body;
If you want a multipart view here: https://www.npmjs.com/package/request#multipartform-data-multipart-form-uploads .
Remember what koa-request
wraps the modulerequest
source to share