使用 multipart/form-data 到 NodeJS 请求的 cURL
cURL to NodeJS Request with multipart/form-data
我需要这样的 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
我试了很多次!请帮忙!
最新版本的代码(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);
以下有效代码:
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' });
我需要这样的 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
我试了很多次!请帮忙!
最新版本的代码(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);
以下有效代码:
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' });