Creat ReadStream object from JSON object Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Creat ReadStream object from JSON object without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am using jira-client npm to deal with jira rest API , and I am trying to add an attachment to a ticket using addAttachmentOnIssue method, this method requires the key of the issue as a string parameter and also requires ReadStream as the second parameter, I am able to attach a file to a ticket if I followed these steps:

var jiraAPI = require('jira-client');
var jira = new JiraApi({
protocol: "https",
host:"myJiraInstance",
username:"myUserName",
password: "MyToken",
apiVersion: "2",
strictSSL: true,})

then

const fileStream = fs.createReadStream(filePath);
jira.addAttachmentOnIssue(tickeID,fileStream);

As you can see I have the filepath and I attached it but I that is not what I want, I want to create a file from JSON object, without writing this file on the system and then send it

is that possible ?

by using:

var stream = require('stream');
var readable = new stream.Readable(); // new empty stream.Readable
readable.push('some data');
readable.push(null);
jira.addAttachmentOnIssue(tciketID,readable)

I am getting:

Processing of multipart/form-data request failed. Stream ended unexpectedly

Answer

the problem is that the required file related information such as filename, knownLength etc. is missing, which is why it fails parsing the stream.

you need to provide file related information manually

as jira-client is using postman-request, you can do that by providing a custom file object like described here: multipart/form-data (Multipart Form Uploads)

Try this:

var JiraApi = require('jira-client');
var jira = new JiraApi({
    protocol: "https",
    host: "myJiraInstance",
    username: "myUserName",
    password: "MyToken",
    apiVersion: "2",
    strictSSL: true
})



const inputData = JSON.stringify({
    someProp: 'some data'
});

var stream = require('stream');
var readable = new stream.Readable();
readable.push(inputData);
readable._read = () => {};
readable.push(null);


// https://www.npmjs.com/package/postman-request#forms
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
// See the `form-data` README for more information about options: https://github.com/form-data/form-data
const myStreamFile = {
    value: readable,
    options: {
        filename: 'json.json',
        contentType: 'application/json',
        knownLength: inputData.length
    }
}


jira.addAttachmentOnIssue(tciketID, myStreamFile)
We are here to answer your question about Creat ReadStream object from JSON object - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji