Vinay | Posts: 4

Creating package with multiple document binaries, uploading with superagent.

0 votes
Hi! I have been trying to upload multiple documents with a post request to '/api/packages' (the sandbox) url and getting getting the error indicating the number of documents sent in the payload is different from what I attached, Could have a look at the code and let me know, what I am missing?
var httpReq = require('superagent');
var _ = require('lodash');

var createPackageReq = httpReq
                                    .post(config.esignature.host + '/packages')
                                    .set('Content-type', 'multipart/form-data')
                                    .set('Accept', 'application/json')
                                   .set(SILANIS_AUTHORIZATION); //my API Key

//req is the request passed on to this function from my UI
_.keys(req.body.documents).forEach(function(docName) {
		documents.push({name: docName});
		createPackageReq
			.attach(docName, req.body.documents[docName].result.path);
});
var stringData = JSON.stringify({documents: documents});
createPackageReq
	.field('payload', stringData)
	..end(function(err, resp) {
			if(err) {
				// console.log(resp.error);
				throw err;
			}
			if (resp.statusCode === 200) {
				// console.log(resp.body);
				res.send(resp.body);
			} else {
				res.sendStatus(resp.statusCode);
			}
		});
and below is the error I am getting,
 [Error: cannot POST /api/packages (400)]
  status: 400,
  text: '{
	"entity":null,
	"messageKey":"error.validation.package.wrongNumberOfFiles",
	"technical":"Number of uploaded files does not match number of documents specified in package.",
	"packageId":null,
	"message":"Number of uploaded files does not match number of documents specified in package.",
	"code":400,
	"name":"Validation Error"
}',
  method: 'POST',
  path: '/api/packages' 
Thanks, Vinay

Approved Answer
harishaidary | Posts: 1812

Reply to: Creating package with multiple document binaries, uploading with superagent.

0 votes
Hey Vinay, Would you be able to share your JSON payload when you are getting this error? Can you also ensure that the payload specifies each document in order as they were added. Here's what your HTTP request should look like when trying to create a package with multiple documents:
POST /api/packages/ HTTP/1.1
Host: sandbox.e-signlive.com
Authorization: Basic your_api_key
Accept: application/json
Cache-Control: no-cache
Postman-Token: e4121be4-f632-387b-f077-415df005e2d5
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename=""
Content-Type: 


----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename=""
Content-Type: 


----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="payload"

{   "documents": [     {       "name": "doc1"     },{       "name": "doc2"     }   ],   "type": "PACKAGE",   "status": "DRAFT",   "name": "testing this!" }
----WebKitFormBoundary7MA4YWxkTrZu0gW

Vinay | Posts: 4

Reply to: Creating package with multiple document binaries, uploading with superagent.

0 votes
Thanks Haris! I figured out the issue, I was giving the name attribute as my file-name instead of 'file' int the form-data attribute.. the code are working good now. Regards, Vinay

Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off