You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Linux迁移至Windows的Curl脚本适配问题求助

解决Windows下curl请求CA证书报错及批量处理方案

一、修复"Content-type not supported"错误

Linux下的curl命令在Windows cmd/PowerShell执行时,核心问题是引号语法差异:Windows cmd不识别单引号,必须用双引号包裹HTTP头参数。修正后的命令如下:

CMD环境适用

curl --connect-timeout 30 --max-time 60 -sk https://ssl.wsgc.com/vedsdk/Certificates/Retrieve -X POST -d @cert-retrieve-labtesttop.wsgc.com.json -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer <Redacted>"

PowerShell环境适用

PowerShell会将@解析为变量符号,需给文件名添加额外引号:

curl --connect-timeout 30 --max-time 60 -sk https://ssl.wsgc.com/vedsdk/Certificates/Retrieve -X POST -d "@cert-retrieve-labtesttop.wsgc.com.json" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer <Redacted>"

二、改用文本文件作为请求体

请求体文件无需强制使用.json后缀,只要文件内容是合法JSON格式,直接用.txt文本文件即可。例如创建cert-request.txt,内容与原JSON文件完全一致,修改后的curl命令:

curl --connect-timeout 30 --max-time 60 -sk https://ssl.wsgc.com/vedsdk/Certificates/Retrieve -X POST -d @cert-request.txt -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer <Redacted>"

注意:确保文本文件编码为UTF-8(无BOM),避免CA服务器解析JSON时出错。

三、批量处理1000+设备的脚本示例

Windows批处理脚本(CMD)

假设你有devices.txt文件(每行一个设备域名,如labtesttop.wsgc.com),脚本会自动生成对应请求体并执行请求:

@echo off
setlocal enabledelayedexpansion

set DEVICE_LIST=devices.txt
set TEMPLATE_FILE=request-template.txt
set AUTH_TOKEN=<Redacted>

for /f "tokens=*" %%i in (%DEVICE_LIST%) do (
    set DEVICE=%%i
    :: 替换模板中的设备名生成请求体
    powershell -Command "(Get-Content %TEMPLATE_FILE%) -replace '{DEVICE_NAME}', '!DEVICE!' | Out-File -Encoding UTF8 'request-!DEVICE!.txt'"
    
    echo 正在请求!DEVICE!的证书...
    curl --connect-timeout 30 --max-time 60 -sk https://ssl.wsgc.com/vedsdk/Certificates/Retrieve -X POST -d @request-!DEVICE!.txt -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer %AUTH_TOKEN%"
    
    del "request-!DEVICE!.txt"
)
endlocal

request-template.txt模板示例(替换为你的实际JSON结构):

{
    "deviceName": "{DEVICE_NAME}",
    "certType": "server",
    "validityDays": 365
}

PowerShell批量脚本(更简洁)

$deviceList = Get-Content "devices.txt"
$template = Get-Content "request-template.txt" -Raw
$authToken = "<Redacted>"
$caUrl = "https://ssl.wsgc.com/vedsdk/Certificates/Retrieve"

foreach ($device in $deviceList) {
    $requestBody = $template -replace "{DEVICE_NAME}", $device
    Write-Host "处理设备:$device"
    # 直接传递字符串请求体,无需临时文件
    curl --connect-timeout 30 --max-time 60 -sk $caUrl -X POST -d $requestBody -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $authToken"
}

内容的提问来源于stack exchange,提问作者Daniel Pierce

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 21:15:06