node.jscurlnode-request

cURL to NodeJS Request with multipart/form-data


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);

Solution

  • 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' });