Home > Net >  Is there an alternative for --data-urlencode only for spaces?
Is there an alternative for --data-urlencode only for spaces?

Time:06-27

I want to create a GET request via CUrl with variables as a batch file. The problem so far has been that the variables can also contain spaces, which does not let the link open in the CMD window.

curl -G "http://192.168.0.100:1234/operations/get?token=abcde12345&subject=var1 in here&object=var2 in here"

I always got the error message bad/illegal format or missing link. Now I have solved the problem with --data-urlencode, however special characters which are contained are also encoded as URL text.

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 ö ß % &"

My code works fine on this website https://reqbin.com/req/c-1n4ljxb9/curl-get-request-example for testing and all special-characters arrive correctly, only in the CMD window it changes these to the url-code...

Is there any way to replace the spaces with the usual " " but leave the special characters unchanged or has someone an alternative for this in the CMD-window? When I enter the URL with special characters in the browser manually, all characters arrive correctly and the spaces are replaced by themselves.

CodePudding user response:

Use the this answer in a batch file (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%

When executing this batch script it will output: Hello World!

Above can be applied to your URL.

CodePudding user response:

The solution was pretty simple with this code:

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%"

Dont use the -G, it will only destroy the code again what happend to me all the time... this code above works, mabybe this will help someone... thanks for the help.

  • Related