【发布时间】:2021-08-22 01:52:28
【问题描述】:
我有以下 cURL 命令:
// original curl
curl https://example.com \
-F "id=id" \
-F "secret=secret"
我认为可以用这个fetch表达式表示:
// fetch
const body = new FormData();
body.append('id', 'id');
body.append('secret', 'secret');
return fetch('https://example.com', {
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'multipart/form-data',
},
body,
})
然后,将获取请求复制为 cURL 生成以下命令:
// generated curl
curl 'https://example.com' \
-H 'content-type: multipart/form-data' \
--data-raw $'------WebKitFormBoundaryH2Ve4S1AUbboJ21W\r\nContent-Disposition: form-data; name="id"\r\n\r\nid\r\n------WebKitFormBoundaryH2Ve4S1AUbboJ21W\r\nContent-Disposition: form-data; name="secret"\r\n\r\nsecret\r\n------WebKitFormBoundaryH2Ve4S1AUbboJ21W--\r\n' \
--compressed
令我惊讶的是,当端点和表单值使用真实数据而不是占位符数据时,原始 curl 请求有效,但生成的 curl 请求无效(获取版本也无效)。
我有什么明显的遗漏吗?原始 cURL 命令和 fetch 表达式 / 生成 cURL 命令有什么区别?
【问题讨论】:
标签: javascript curl fetch