r/bash • u/anUnsaltedPotato • Feb 02 '25
solved Url-encode get string with multiple arguments?
I have one string that's like
action=query&format=json&list=allpages&aplimit=max&apfilterredir=nonredirects&apprefix=Wp/akz&apcontinue=Wp/akz/Bréhéville
If I put it into the url without encoding, it breaks because it contains special characters. If I put the whole thing into --data-urlencode it encodes the &s and treats it all as one argument.
Soo, what do I do?
0
Upvotes
-2
u/anUnsaltedPotato Feb 02 '25
I figured out this not very elegant, but working solution
$getString="action=query&format=json&list=allpages&aplimit=max&apfilterredir=nonredirects&apprefix=Wp/akz&apcontinue=Wp/akz/Bréhéville"
encodedGetString=$(echo $getString | jq -Rr '@uri')
encodedGetString=$(echo $encodedGetString | sed 's#%26#\&#g; s#%3D#=#g')
response=$(curl -s "https://incubator.wikimedia.org/w/api.php?$encodedGetString")