我想通過 CUrl 創建一個帶有變數作為批處理檔案的 GET 請求。到目前為止的問題是變數也可以包含空格,這不會讓鏈接在 CMD 視窗中打開。
curl -G "http://192.168.0.100:1234/operations/get?token=abcde12345&subject=var1 in here&object=var2 in here"
我總是收到錯誤訊息錯誤/非法格式或缺少鏈接。現在我用 --data-urlencode 解決了這個問題,但是包含的特殊字符也被編碼為 URL 文本。
curl -G "http://192.168.0.100:1234/operations/get" --data-urlencode "token=abcde12345" --data-urlencode "subject=var1 in here" --data-urlencode "object=var2 has some spechial chars ? ? % &"
我的代碼在這個網站上運行良好https://reqbin.com/req/c-1n4ljxb9/curl-get-request-example用于測驗并且所有特殊字符都正確到達,只有在 CMD 視窗中它才會將這些更改為 url-代碼...
有什么方法可以用通常的“ ”替換空格,但保持特殊字符不變,或者在 CMD 視窗中有替代方法?當我在瀏覽器中手動輸入帶有特殊字符的 URL 時,所有字符都正確到達,并且空格被自己替換。
uj5u.com熱心網友回復:
在批處理檔案中使用此答案(https://stackoverflow.com/a/29945186/724039):
echo off
set "str1=Hello World!"
set "str2=% "
for /f "delims=" %%a in ('cmd /v:on /c @echo "%%str1: =!str2!%%"') do set "str3=%%~a"
echo %str3%
執行此批處理腳本時,它將輸出:Hello World!
以上可以應用于您的網址。
uj5u.com熱心網友回復:
使用此代碼的解決方案非常簡單:
set "str1=http://192.168.0.100:1234/operations/get?token=abcde12345&subject=var1 in here&object=var2 in here with ? ?"
set "str2=% "
for /f "delims=" %%a in ('cmd /v:on /c @echo "%%str1: =!str2!%%"') do set "str3=%%~a"
curl "%str3%"
不要使用-G,它只會再次破壞我一直發生的代碼......上面的代碼有效,也許這會對某人有所幫助......感謝您的幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/496903.html
