Is there an alternative for --data-urlencode only for spaces?

311 Views Asked by At

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

2

There are 2 best solutions below

1
Luuk On BEST ANSWER

Use the this answer in a batch file (https://stackoverflow.com/a/29945186/724039):

echo off
set "str1=Hello World!"
set "str2=%%20"
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%20World!

Above can be applied to your URL.

0
Josh51 On

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

for /f "delims=" %%a in ('cmd /v:on /c @echo "%%str1: =!str2!%%"') do set "str3=%%~a"

curl "%str3%"

Don't use the -G, it will only destroy the code again what happened to me all the time....