node.js - How to make a form-data request with koa? -
i trying replicate login form's behaviour through koa.
the login form does:
<form id="loginform" method="post" action="http://myaddress:3000/auth" enctype="multipart/form-data">
i'm using koa request , 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 400 response.
i've tried using form.submit(db_server_url + 'auth', function(err, res) { ... }
works, koa's yield functionality , ideally want avoid having deal callbacks.
any ideas?
koa accepts multiple yield inputs can obtained current code more or less depending on current setup:
a promise. form-data doesn't seem utilize them, we'll create 1 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, wrapper function used in answer, there libraries can handle wrapping (here, thunkify-wrap):
var thunkify = require('thunkify-wrap'); var submit = thunkify(form.submit, form); // context needed in case var response = yield submit(db_server_url + 'auth'); console.log('response.statuscode: ' + response.statuscode);
node.js multipartform-data koa
No comments:
Post a Comment