带 -F 选项的 cURL 到带有 Axios 的 NodeJS
cURL with -F Option into NodeJS with Axios
我正在尝试转换此 CURL 命令
curl -X POST "https://serverless-upload.twilio.com/v1/Services/ZS5798711f7bee1284df67427071418d0b/Assets/ZH4912f44da25f4b1a1c042a16a17f2eac/Versions" \
-F Content=@./mapping/mapping.json; type=application/json \
-F Path=mapping.json \
-F Visibility=private \
-u username:password
使用包 axios 进入 post 请求,
我试过了
url = `https://serverless-upload.twilio.com/v1/Services/${service_uid}/Assets/${asset_uid}/Versions`
data = {
'Path': 'mapping.json',
'Visibility': 'private',
'Content': JSON.stringify(mapping),
'filename': 'mapping.json',
'contentType': 'application/json'
}
await axios.post(url, data, {
auth : {
user: `${accountSid}:${authToken}`
},
headers: {
'Content-Type': 'multipart/form-data',
}
}).then((r) => console.log(r));
但我不确定这是否格式错误
这里是 Twilio 开发人员布道者。
你可以用Twilio Node library actually uses axios under the hood, you can see it in action in the RequestClient. We also have a stand-alone Serverless API client which is part of the Twilio Serverless Toolkit,但是写的是got
。
您可以使用 Serverless API 模块来省去重新创建此请求的工作。
如果您决定继续使用 axios,请进行以下更改。
授权
授权是通过 Authorization
header 完成的,传递由帐户 Sid 和授权令牌组成的 base 64 编码字符串。
headers: {
Authorization: 'Basic ' + Buffer.from(`${accountSid}:${authToken}`).toString('base64')
}
数据
上传资产时,它是作为多部分表单数据完成的。要在 Node.js 中构建多部分数据,您可以使用 form-data module。这样的事情应该有效:
const FormData = require("form-data");
const form = new FormData();
form.append("Path", "mapping.json");
form.append("Visibility", "private");
form.append("Content", JSON.stringify(mapping));
form.append("filename", "mapping.json");
form.append("contentType", "application/json");
await axios.post(url, form, {
headers: {
Authorization: 'Basic ' + Buffer.from(`${accountSid}:${authToken}`).toString('base64'),
...form.getHeaders(),
},
}).then((r) => console.log(r));
告诉我你是怎么处理的。
我正在尝试转换此 CURL 命令
curl -X POST "https://serverless-upload.twilio.com/v1/Services/ZS5798711f7bee1284df67427071418d0b/Assets/ZH4912f44da25f4b1a1c042a16a17f2eac/Versions" \
-F Content=@./mapping/mapping.json; type=application/json \
-F Path=mapping.json \
-F Visibility=private \
-u username:password
使用包 axios 进入 post 请求,
我试过了
url = `https://serverless-upload.twilio.com/v1/Services/${service_uid}/Assets/${asset_uid}/Versions`
data = {
'Path': 'mapping.json',
'Visibility': 'private',
'Content': JSON.stringify(mapping),
'filename': 'mapping.json',
'contentType': 'application/json'
}
await axios.post(url, data, {
auth : {
user: `${accountSid}:${authToken}`
},
headers: {
'Content-Type': 'multipart/form-data',
}
}).then((r) => console.log(r));
但我不确定这是否格式错误
这里是 Twilio 开发人员布道者。
你可以用Twilio Node library actually uses axios under the hood, you can see it in action in the RequestClient. We also have a stand-alone Serverless API client which is part of the Twilio Serverless Toolkit,但是写的是got
。
您可以使用 Serverless API 模块来省去重新创建此请求的工作。
如果您决定继续使用 axios,请进行以下更改。
授权
授权是通过 Authorization
header 完成的,传递由帐户 Sid 和授权令牌组成的 base 64 编码字符串。
headers: {
Authorization: 'Basic ' + Buffer.from(`${accountSid}:${authToken}`).toString('base64')
}
数据
上传资产时,它是作为多部分表单数据完成的。要在 Node.js 中构建多部分数据,您可以使用 form-data module。这样的事情应该有效:
const FormData = require("form-data");
const form = new FormData();
form.append("Path", "mapping.json");
form.append("Visibility", "private");
form.append("Content", JSON.stringify(mapping));
form.append("filename", "mapping.json");
form.append("contentType", "application/json");
await axios.post(url, form, {
headers: {
Authorization: 'Basic ' + Buffer.from(`${accountSid}:${authToken}`).toString('base64'),
...form.getHeaders(),
},
}).then((r) => console.log(r));
告诉我你是怎么处理的。