我有以下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命令有什么区别?
发布于 2021-06-04 00:24:28
我相信你的目标如下。
fetch
。
curl https://example.com \ -F "id=id“\ -F "secret=secret”在本例中,下面的脚本如何?当使用FormData
时,Content-Type
会通过包含边界自动添加到请求头中。
示例脚本:
const body = new FormData();
body.append('id', 'id');
body.append('secret', 'secret');
return fetch('https://example.com', {
method: 'POST',
// mode: 'no-cors' // I thought that this might not be required to be used. But please check this for your actual situation.
body
});
参考资料:
添加:
关于你下面的评论,
您知道如何将原始的cURL命令转换为不使用-F选项的东西吗?
在这种情况下,手动创建请求体如何?
curl -H 'Content-Type: multipart/form-data; boundary=boundaryboundary' \
-d $'--boundaryboundary\r\nContent-Disposition: form-data; name="id"\r\n\r\nid\r\n--boundaryboundary\r\nContent-Disposition: form-data; name="secret"\r\n\r\nsecret\r\n--boundaryboundary--\r\n' \
'https://example.com'
https://stackoverflow.com/questions/67829949
复制相似问题