cURL to NodeJS Request with multipart/form-data - node.js

i need make request like this cURL:
curl -u [staff_email]:[api_key] -F "case[attachments][0]=#/path/to/file1.ext" -F "case[content]=I need help" -F "case[subject]=I need help" -F "case[user_email]=user#domain.ru" -F "case[user_full_name]=FullName" -F "case[language_id]=1" -X POST https://[domain].omnidesk.ru/api/cases.json
i try a lot of times! Pls, help!
last version of code (res: string[]/csv):
const auth = 'Basic ' + Buffer
.from('user:password')
.toString('base64');
const query = {
'case[subject]': "subject",
'case[content]': 'text',
'case[user_email]': 'someemail#gmail.com',
'case[user_full_name]': 'some_name',
'case[group_id]': 18278,
'case[language_id]': 1,
'case[attachments][0]': Buffer.from(res.join("\r\n"), 'utf8'),
};
const cb = (e, r, b) => {
console.log(e, r, b);
};
const options = {
method: 'post',
url: 'https://domain.omnidesk.ru/api/cases.json',
headers: {
"Authorization": auth,
"Content-Type": "multipart/form-data"
},
form: query,
};
request(options, cb);

worked code below:
const query = {
'case[subject]': "subject",
'case[content]': 'content',
'case[user_email]': 'tt#tt.tt',
'case[user_full_name]': 'some name',
'case[language_id]': 1
};
const cb = (e, r, b) => {
console.log(e, b);
};
const options = {
method: 'POST',
url: 'https://domain.omnidesk.ru/api/cases.json',
auth: {
user: 'login',
pass: 'password'
},
qs: query
};
const req = request(options, cb);
const form = req.form();
form.append('case[attachments][0]', res.join("\r\n"), { filename: 'some_file.csv' });

Related

Node Js get request in for loop and collect responses in an object

nodejs / meteorjs newbie here,
I am trying to query an api in for loop and collect all responses in one object and return that object. here is my code set up
const req = require('request');
const getallJira = (namespace, services) => {
let allJiraResponses = {};
for (let i in services) {
let _serviceName = services[i];
const options = {
method: 'POST',
url: 'https://jira/jira/rest/api/2/search',
headers: {
'Content-Type': 'application/json',
Authorization: base64Auth,
Accept: 'application/json',
},
body: JSON.stringify({
jql: `NAMESPACE = ${namespace} AND labels = 'kind/promotion' AND (SERVICE_NAME = '${_serviceName}' OR 'Service Name/Package Name' = '${_serviceName}')`,
maxResults: 1000000,
startAt: 0,
fields,
}),
};
request(options, function(error, response) {
if (error) throw new Error(error);
const jiraResponse = JSON.parse(response.body);
allJiraResponses[_serviceName] = jiraResponse;
});
}
return allJiraResponses;
};
however the final allJiraResponses is returned empty because of the asynchronous nature of request. How do i record all the responses from jira API in the object and return it to the caller of getallJira
I think you should separate into function to make it clearer
the first one to get the result for a single service
const getJiraService = ( namespace, serviceName) => new Promise((resolve, reject) => {
const options = {
method: 'POST',
url: 'https://jira/jira/rest/api/2/search',
headers: {
'Content-Type': 'application/json',
Authorization: base64Auth,
Accept: 'application/json',
},
body: JSON.stringify({
jql: `NAMESPACE = ${namespace} AND labels = 'kind/promotion' AND (SERVICE_NAME = '${serviceName}' OR 'Service Name/Package Name' = '${serviceName}')`,
maxResults: 1000000,
startAt: 0,
fields,
}),
};
request(options, function(error, response) {
if (error) {
resolve({serviceName, error});
return;
}
const jiraResponse = JSON.parse(response.body);
resolve({serviceName, jiraResponse})
});
})
const getallJira = async (namespace, services) => {
const results = await Promise.all(services.map(s => getJiraService(namespace, s));
return results.reduce((res, {serviceName, error, jiraResponse}) => {
if(!!jiraResponse){
return {
...res,
serviceName: jiraResponse
};
}
return res;
}, {})

what is the nodejs code for this curl command

I can successfully execute this curl command from a Unix shell script and I can see output in C:\Users\OutputFile.csv. What is the equivalent code in NodeJS
curl -k -v --user 'helloworld:hello_password'
--header 'Accept: application/vnd.myDMS-dms-api+json; version=1'
-X POST 'https://DMS.com:3001/download/csv'
--data header=true -o C:\Users\OutputFile.csv
I tried using the Online curl to nodeJS converter and it has generated the following NodeJs code:-
var request = require('request');
var headers = {
'Accept': 'application/vnd.myDMS-dms-api+json; version=1'
};
var options = {
url: 'https://DMS.com:3001/download/csv',
method: 'POST',
headers: headers,
auth: {
'user': 'helloworld',
'pass': 'hello_password'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
However, when I run this NodeJS code it does not show any output. Also how can I get this output to C:\Users\OutputFile.csv
Maybe the response isn't return before the script is terminated. You would want the request to be asynchronous:
You can use request-promise
Here's an example
var rp = require('request-promise');
function someFunction() {
let options = {
url: `someURL`,
method: 'POST',
body: {
some: 'payload'
},
json: true
};
return rp(options);
}
This will await the response.
A simple version of your API parameters using request-promise:
var rp = require('request-promise');
function downloadFile() {
var options = {
uri: 'https://DMS.com:3001/download/csv',
method: 'POST',
auth: {
user: 'helloworld',
pass: 'hello_password',
sendImmediately: true
},
headers: {
Accept:'application/vnd.myDMS-dms-api+json; version=1'
},
form: {
'header': 'true'
}
};
rp(options)
.then(function (body) {
console.log('Downloaded body was %d long', repos.length);
})
.catch(function (err) {
console.log(err)
});
}
downloadFile()

How to correctly handle Params and Querys in Axios?

I'm consuming an API with NODEJS and using the axios to do the "get" ...
In NODEJS we call "params" everything that comes before the "?" Character, and we call the "query" everything that goes after the "?" Character, such as:
https://www.url.com/testing.xsjs?QueryName1='test1,test1'&QueryName2=1
The problem I'm having in Axios is that it does not create the url correctly, which the correct URL should be:
https: //www.url.comho/fatSales.xsjs?Shop='shop1,shop2,shop3'&PeriodoDe=201801&PeriodoAte=201807&Kpi='kp1,kp2,kp3'& Select = NUCOMPETEC
But the url he is creating for me is this:
https://www.apiUrl.com/Shop=shop1, + shop2, + shop3&PeriodoDe=201801&PeriodoAte=201807&Kpi=kp1,+kp2,+kp3&Select=NUCOMPETEC
I have some issues with this URL that it creates that are as follows:
1) Shop and Kpi it creates the "+" character
2) It does not add the parameter (NODEJS) before the "?" Character ...
Remembering that:
Shop and Kpi (It's an array with 1 or * elements)
const axios = require('axios');
const Qs = require('qs');
class ApiDAO {
constructor(xsjs, shoppingId, periodOf, periodUntil, kpi, select){
this.xsjs = xsjs;
this.shoppingId = shoppingId;
this.periodOf = periodOf;
this.periodUntil = periodUntil;
this.kpi = kpi;
this.select = select;
}
configAxios(){
return axios.create({
method: 'GET',
responseType: 'json',
responseEncoding: 'utf8',
headers: {
'Content-Type': "application/json",
'Cache-Control': "no-cache",
Authorization: "",
Apikey: "",
},
params: {
xsjs: this.xsjs,
Shop: this.shoppingId,
PeriodoDe: this.periodOf,
PeriodoAte: this.periodUntil,
Kpi: this.kpi,
Select: this.select
},
});
}
async getResponseAxios(){
return await this.configAxios().get('https://www.apiUrl.com/');
}
}
module.exports = () => { return ApiDAO };
Are you stringifying your params? Can you try the following:
const axios = require('axios');
const Qs = require('qs');
class ApiDAO {
constructor(xsjs, shoppingId, periodOf, periodUntil, kpi, select){
this.xsjs = xsjs;
this.shoppingId = shoppingId;
this.periodOf = periodOf;
this.periodUntil = periodUntil;
this.kpi = kpi;
this.select = select;
}
configAxios(url){
return axios.get({
method: 'GET',
url: url,
responseType: 'json',
responseEncoding: 'utf8',
headers: {
'Content-Type': "application/json",
'Cache-Control': "no-cache",
Authorization: "",
Apikey: "",
},
params: {
xsjs: this.xsjs,
Shop: this.shoppingId,
PeriodoDe: this.periodOf,
PeriodoAte: this.periodUntil,
Kpi: this.kpi,
Select: this.select
},
});
}
async getResponseAxios(){
return await this.configAxios('https://www.apiUrl.com/');
}
}
module.exports = () => { return ApiDAO };
Or if you want to use axios create, pass the URL in earlier as an option field. I don't think it's an error with the other params.

How do you make this curl request in node

This curl request to the spotify API works perfectly fine
curl -X "POST" -H "Authorization: Basic <my-key-here>" -d grant_type=client_credentials https://accounts.spotify.com/api/token
I'm trying to do this in node, but it's not working and returns a 400 Bad Request. Here is my code. What am I doing wrong?
function AuthRequest(key) {
const req_body_params = JSON.stringify({
grant_type: "client_credentials"
})
const base64_enc = new Buffer(key).toString("base64")
const options = {
host: "accounts.spotify.com",
port: 443,
path: "api/token",
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Basic ${base64_enc}`
}
}
const req = https.request(options, function (res) {
res.on('data', function (data) {
alert("success: " + data)
})
})
req.on('error', function (err) {
alert("error: " + err)
})
req.write(req_body_params)
req.end()
}
I'm trying to use the Client Credentials method as explained here: https://developer.spotify.com/web-api/authorization-guide/
The request should be in application/x-www-form-urlencoded instead of JSON, it should be const req_body_params = "grant_type=client_credentials" with a header of "Content-Type": "application/x-www-form-urlencoded"
function AuthRequest(key) {
const req_body_params = "grant_type=client_credentials";
const base64_enc = new Buffer(key).toString("base64");
const options = {
host: "accounts.spotify.com",
port: 443,
path: "/api/token",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": `Basic ${base64_enc}`
}
};
const req = https.request(options, function(res) {
res.on('data', function(data) {
alert("success: " + data)
})
});
req.on('error', function(err) {
alert("error: " + err)
});
req.write(req_body_params);
req.end();
}
Because token expires is good to request them dynamically, I created a method that output a token as a promise, then you can consume in your request.
const axios = require('axios')
const client_id = process.env.SPOTIFY_CLIENT_ID;
const client_secret = process.env.SPOTIFY_CLIENT_SECRET;
function getToken( ) {
return axios({
url: 'https://accounts.spotify.com/api/token',
method: 'post',
params: {
grant_type: 'client_credentials'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
auth: {
username: client_id,
password: client_secret
}
})
}
async function getSpotifyData( endpoint ){
const tokenData = await getToken( );
const token = tokenData.data.access_token;
axios({
url: `https://api.spotify.com/v1/${endpoint}`,
method: 'get',
headers: {
'Authorization': 'Bearer ' + token
}
}).then( response => {
console.log( response.data );
return response.data;
}).catch( error => {
throw new Error(error);
});
}
getSpotifyData( 'browse/new-releases' );

Nodejs Post attachment to JIRA

I am receiving http POST response OK 200, but I see no file present on JIRA issue. From my research I can understand that it could be some problem with formData I am sending with request. Below is my code:
var newBuffer = new Buffer(req.Payload, 'base64');
var myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({
frequency: 10, // in milliseconds.
chunkSize: 2048 // in bytes.
});
// With a buffer
myReadableStreamBuffer.put(newBuffer);
var formData = {
'file': {
'content': myReadableStreamBuffer,
'filename': req.FileName,
'mimeType': req.MimeType //mimeType from JSON
}
};
var options = {
url: 'https://comapny.atlassian.net/rest/api/2/issue/' + req.ReferenceId + '/attachments',
method: "POST",
json: true,
headers: {
'ContentType': 'multipart/form-data',
'Authorization': 'Basic ' + new Buffer(config.jira.jiraUser.userName + ':' + config.jira.jiraUser.password).toString('base64'),
'X-Atlassian-Token': 'nocheck'
},
formData: JSON.stringify(formData)
};
request(options,
function (error, response, body) {
if (error) {
errorlog.error(`Error Message : PostAttachmentToCSMS : ${error}`);
return response.statusCode;
}
else {
successlog.info(`Attachment posted for issue Key: ${req.ReferenceId} ${response.statusMessage}`);
return response.statusCode;
}
});
I can write file from myReadableStreamBuffer, so that seems ok. Please help me to identify the problem. Many thanks!
After spending some more time on it, I have found the correct format for formData:
var newBuffer = new Buffer(req.Payload, 'base64');
var formData = {
file: {
value: newBuffer,
options: {
filename: req.FileName,
contentType: req.MimeType
}
}
};
For whom like me getting errors with this API.
After struggling so many hrs on this thing, I finally found this works like a charm. I've got "XSRF check failed" 403/404 error message before writing this code.
// Don't change the structure of formData.
const formData = {
file: {
value: fs.createReadStream(filepath),
options: {
filename: filename,
contentType: "multipart/form-data"
}
}
};
const header = {
"Authentication": "Basic xxx",
// ** IMPORTANT **
// "Use of the 'nocheck' value for X-Atlassian-Token
// has been deprecated since rest 3.0.0.
// Please use a value of 'no-check' instead."
"X-Atlassian-Token": "no-check",
"Content-Type": "multipart/form-data"
}
const options = {
url: "http://[your_jira_server]/rest/api/2/issue/[issueId]/attachments",
headers: header,
method: "POST",
formData: formData
};
const req = request(options, function(err, httpResponse, body) {
whatever_you_want;
};
I was able to post attachments to JIRA using axios in the following way:
const axios = require('axios');
const FormData = require('form-data')
const fs = require('fs');
const url = 'http://[your_jira_server]/rest/api/2/issue/[issueId]/attachments';
let data = new FormData();
data.append('file', fs.createReadStream('put image path here'));
var config = {
method: 'post',
url: url,
headers: {
'X-Atlassian-Token': 'no-check',
'Authorization': 'Basic',
...data.getHeaders()
},
data: data,
auth: {
username: '',
password: ''
}
};
axios(config)
.then(function (response) {
res.send({
JSON.stringify(response.data, 0, 2)
});
})
.catch(function (error) {
console.log(error);
});

Resources