如何在windows cmd中将包含空格字符的字符串变量传递给curl命令

cotxawn7  于 2022-11-13  发布在  Windows
关注(0)|答案(1)|浏览(328)

如何将一个带有空格字符的字符串传递给一个变量以在windows cmd中执行curl命令?
我必须在windows cmd中传递一个包含白色字符的字符串变量来curl请求。下面是命令。

set emailID="xxxx@gmail.com"
set openAPISpec="http://petstore/v2/swagger.json"
set licenseKey="ccc0e4-b000-491f-9d7b-7e59f9768"

set projectName="Online Banking REST API OmPf"

set MyDefault="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyDefault"
set MyRoleAdmin="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleAdmin"
set MyRoleUser="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleUser"

curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" -d "{     \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"%projectName%\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"

当我在windows cmd中运行上述命令时,以下是响应

curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" -d "{     \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"%projectName%\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"

{"timestamp":"2022-10-19T03:03:39.094+00:00","status":400,"error":"Bad Request","path":"/api/v1/scan"}

如果变量没有空格字符,windows CMD中的上述命令可以正常工作。
同样的错误也出现在Linux bash中,所以我根据Linux环境修改了上面的命令,即使变量中有空格字符,它也能完美地工作。
以下是Linux中变量中包含空格字符时的工作命令。

export emailID="xxxx@gmail.com"
export openAPISpec="http://petstore/v2/swagger.json"
export licenseKey="ccc0e4-b000-491f-9d7b-7e59f9768"

export projectName="Online Banking REST API OmPf"

export MyDefault="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyDefault"
export MyRoleAdmin="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleAdmin"
export MyRoleUser="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleUser"

curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" --data-raw '{     "openAPISpec": "'${openAPISpec}'", "email": "'${emailID}'", "licenseKey": "'${licenseKey}'", "projectName": "'"${projectName}"'", "headers":["Authorization: Bearer '${MyDefault}'","Authorization: Bearer '${MyRoleAdmin}'","Authorization: Bearer '${MyRoleUser}'"] }'

下面是在Linux bash中实际工作的东西,我已经添加了额外的双引号来处理带有projectName变量的空格字符,而带有email变量的空格字符是正常的。

"projectName": "'"${projectName}"'"

"email": "'${emailID}'"

相同的修改在Windows CMD中不起作用,或者目前,我无法弄清楚

#1 Tried to pass this way
\"projectName\": \"\'\"%projectName%\"\'\" 

# Command
curl -s --location --request POST "https://api.ethicalcheck.apisec.ai/api/v1/scan" -H "Content-Type: application/json" -d "{     \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"\'\"%projectName%\"\'\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"

# Response
{"timestamp":"2022-10-19T03:03:39.094+00:00","status":400,"error":"Bad Request","path":"/api/v1/scan"}

#2 Tried to pass this way
\"projectName\": \"\"%projectName%\"\" 

  # Command
    curl -s --location --request POST "https://api.ethicalcheck.apisec.ai/api/v1/scan" -H "Content-Type: application/json" -d "{     \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"\"%projectName%\"\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"

  # Response
    {"timestamp":"2022-10-19T03:03:39.094+00:00","status":400,"error":"Bad Request","path":"/api/v1/scan"}

那么,我如何将一个带有空格字符的字符串作为变量传递给windows CMD中的curl命令呢?

ax6ht2ek

ax6ht2ek1#

这个链接why-is-no-string-output-with-echo-var-after-using-set-var-text-command-line帮助解决了这个问题。谢谢@Mofi
下面的语法对我很有效。

set "emailID=xxxx@gmail.com"
set "openAPISpec=http://petstore/v2/swagger.json"
set "licenseKey=ccc0e4-b000-491f-9d7b-7e59f9768"

set "projectName=Online Banking REST API OmPf"

set "MyDefault=dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyDefault"
set "MyRoleAdmin=dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleAdmin"
set "MyRoleUser=dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleUser"

curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" -d "{     \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"%projectName%\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"

相关问题