Open bialesdaniel opened 8 years ago
@bialesdaniel - multipart form data is not supported at the moment. Care to PR?
Sure I can try!
@bialesdaniel Any fixes for multipart form data?
I have tried storing the contents of the excel file as form value and calling the api....I get unsupported media type.........Inspite of setting content type header to multipart form data........while calling the api it is taking as applicaiton/json.
@bialesdaniel any update on this?
Sorry I lost track of this. I need to write tests for the code but Here is what I had (it was a long time ago so I can't remember if it was working or not)
Apickli.prototype.setFormData = function (formDataTable) {
var self = this;
var formDataObject = {};
formDataTable.forEach(function (f) {
var formDataName = self.replaceVariables(f.name);
var formDataValue = self.replaceVariables(f.value);
var formDataType = self.replaceVariables(f.type);
if (formDataType.toLowerCase() === 'file') {
formDataObject[formDataName] = fs.createReadStream(self.fixturesDirectory + formDataValue);
} else {
formDataObject[formDataName] = formDataValue;
}
});
this.formData = formDataObject;
}
Then you just need this.formData ={}
in the constructor and add formData:this.formData
in the Apickli.prototype.post
method. I'll see if I can get back to this in the next week or two.
Is multipart/form-data now being handled?
@bialesdaniel is the above code has been tested?
@sri-asb Sorry I lost the code I was working on. I never created the PR with the code and tests but I was using the code above for a while and it worked
I was able to create the following step definition we use now in our tests:
Given('I set file {string} as multipart form-data with mime-type {string}', function(fileName, mimeType, callback)
{
this.apickli.addRequestHeader('Content-Type', 'multipart/form-data');
if (fs.existsSync(fileName))
{
const formData = fs.readFileSync(fileName);
this.apickli.httpRequestOptions.multipart = {
chunked: false,
data: [
{
body: formData, 'content-type': mimeType
}]
};
callback();
}
else
{
callback(new Error(`File ${fileName} does not exist`));
}
});
This works, because the options are just passed on to the creating of a request (https://github.com/request/request), and this has support for multipart form data.
Hi I tried using the pipe file to body but I don't think that does exactly what I need to do. The options for the request that I am trying to test would have to look something like: