I have a URL that I curl in a bash function, and it is appended by a string parameter. The URL has backslashes in it, and they work fine. However, if there is a backslash in the parameter, it fails.
The important part is:
curl "http://latex.codecogs.com/png.latex?\bg_white&space;\huge&space;$1" > image.png
Now, if the parameter is "f(x)=x^{2}" it'll work fine. But if I try "f(x)=\sqrt{x}" then I only get back the part before the backslash. I've tried single and double quotes on both parts, but nothing is working.
How do I pass a parameter with a backslash and then concatenate it with another string and still maintain the backslash?
Try replacing your backslash into their url encoded format %5C
f(x)=%5Csqrt{x}
The comment by #benjamin-w in the question worked:
curl -G "http://latex.codecogs.com/png.latex" --data-urlencode '\bg_white&space;\huge&space;'"$1" > image.png
Related
I have the following data. Which is not valid in YAML. Double quotes are not working here:
data:"{\gateway\: \172.16.16.1/24\ \modulesModel\: [\N9K-X9364v\ \N9K-vSUP\]}"
Is there any better way to format it?
You are starting escape sequences with the backslash.
Use \\ to represent the backslash in a yaml string.
However your string looks like JSON where " are replaced with \ and , are missing completely. Maybe somebody told you to Escape the " with \. This is done this way:
data: "{\"gateway\": \"172.16.16.1/24\", \"modulesModel\": [\"N9K-X9364v\" \"N9K-vSUP\"]}"
lifecycle:
preStop:
exec:
command: ["sh", "-c", "curl -v -X PUT -d '\"SHUTTING_DOWN\"' http://localhost:8080/v1/info/state"]
I am expecting this will produce a curl url like
curl -v -X PUT -d '"SHUTTING_DOWN"' http://localhost:8080/v1/info/state
How ever I am getting with extra single quotes surrounded ''"SHUTTING_DOWN"''
curl -v -X PUT -d ''"SHUTTING_DOWN"'' http://localhost:8080/v1/info/state
Any pointers, where am I going wrong?
I'd suggest getting rid of as many layers of quotes as you can. In the original example you have a layer of quotes from YAML, plus a layer of quotes from the sh -c wrapper. Since you need the HTTP PUT body itself to have both single and double quotes – you need to send the string '"SHUTTING_DOWN"' with both kinds of quotes over the wire – getting rid of as much quoting as you can is helpful.
In both the shell and YAML, the two kinds of quotes behave differently. Backslash escaping only works in double-quoted strings and so you probably need that at the outer layer; then you need single quotes inside the double quotes; and then you need backslash-escaped double quotes inside that.
In YAML specifically the quotes around strings are usually optional, unless they're required to disambiguate things (forcing 'true' or '12345') to be strings. This lets you get rid of one layer of quoting. You also may find this slightly clearer if you use YAML block style with one list item on a line.
command:
- /bin/sh
- -c
- curl -v -X PUT -d "'\"SHUTTING_DOWN\"'" http://localhost:8080/v1/info/state
I might even go one step further here, though. You're not using environment variable expansion, multiple commands, or anything else that requires a shell. That means you don't need the sh -c wrapper. If you remove this, then the only layer of quoting you need is YAML quoting; you don't need to worry about embedding a shell-escaped string inside a YAML-quoted string.
You do need to make sure the quotes are handled correctly. If the string begins with a ' or " then YAML will parse it as a quoted string, and if not then there are no escaping options in an unquoted string. So again you probably need to put the whole thing in a double-quoted string and backslash-escape the double quotes that are part of the value.
Remember that each word needs to go into a separate YAML list item. curl like many commands will let you combine options and arguments, so you can have -XPUT as a single argument or -X and PUT as two separate arguments, but -X PUT as a single word will include the space as part of that word and confuse things.
command:
- curl
- -v
- -X
- PUT
- -d
- "'\"SHUTTING_DOWN\"'"
- http://localhost:8080/v1/info/state
For example, using two of the backslashes don't seem to help:
curl "https://www.somewebsitethatjustkeepsgoing.com/jflawfelaifjliajsefaslaslfajslie&alksdjf \\
?andsomemore=2235233 \\
--output my.pdf
A double backslash simply means "put a literal backslash here". If you want to extend a url across multiple lines, just use a single backslash...and no whitespace. For example:
curl goo\
gle.com
Hi I have a file which has a string in it that is in an array format and i'm using jq to loop through each index and have some logic depends on the string on each index. However there is a backslash character and jq returns invalid escape character. So I think I need to escape the backlash. How can I do that using sed?
Example:
["*","*Cases","*/opt/selenium/tests/src/test/cases","*../../src/test/cases","*01 Login","*/opt/selenium/tests/src/test/cases/01_login.robot","*../../src/test/cases/01_login.robot","*TC001 Verify the login","*<p>If member has clicked 'Remember Me' check box, after the deployment, it's not necessary to input account and password when login. Web do not have the 'Remember Me' function\x3c/p>","*Login button is not visible after 60 seconds"]
You wrote you want to escape any backslash (\), not slash (/). Assuming that the escape character is backslash itself, then the following should do the job:
sed -i 's/\\/\\\\/g' filename
This can become even shorter:
sed -i 's/\\/&&/g' filename
but using literals in the replacement instead of the variable & is more efficient.
In case you just made a kind of typo, and you want instead to escape slashes with backslashes, then the following command should suffice:
sed -i 's|/|\\/|g' filename
(Since I used | instead of /, I don't need to escape the / in the replacement.)
You can escape / with \/ in sed like this:
sed -i "s#\/#\\\/#g" filename
The error is probably caused by the escape sequence \x3c appearing in one of the string literals.
That is a valid escape sequence in JavaScript (it represents the character <), but in JSON it is invalid.
JSON does support Unicode escapes, though: \u003c.
Apparently you got your input from some kind of JavaScript serializer.
The best solution would of course be to replace that by a proper JSON serializer.
If that is not possible or not practical (for example because it comes from a third party),
then you could use sed to replace every \xnn with its Unicode counterpart \u00nn.
sed 's/\\x\([0-9A-Fa-f]\{2\}\)/\\u00\1/g' poorjson.txt
Output:
["*","*Cases","*/opt/selenium/tests/src/test/cases","*../../src/test/cases","*01 Login","*/opt/selenium/tests/src/test/cases/01_login.robot","*../../src/test/cases/01_login.robot","*TC001 Verify the login","*<p>If member has clicked 'Remember Me' check box, after the deployment, it's not necessary to input account and password when login. Web do not have the 'Remember Me' function\u003c/p>","*Login button is not visible after 60 seconds"]
Please note that this is not 100% foolproof.
It does not check if the backslash itself has not been escaped.
I am trying to pass parameters to cURL through the command line, this way:
curl -s -X POST -H "Content-Type: text/xml" -H "Cache-Control: no-cache" -d '<Data Token="someToken" Name='"$appName"' ID='"$someVar"' ParseAppID='"$someVar"' ParseRESTKey='"$someVar"' AndroidPackage='"$someVar"' Version="1"></Data>' 'https://prefix.something.com/somePath?InputType=Xml'
(This line is actually extracted from the Postman app).
I Googled this issue and found whole lot of solutions that did not work for me (links are to SO past questions...):
I tried isolating the variables by ending the single quotes, this way: 'before...'"${someVar}"'...after...'. Could not complete the request.
I tried passing the variables using a file (-d #fileName). Failed to post.
I tried replacing the single quotes surrounding the <Data> tokens with double quotes - but the command apparently cannot accept such substitution.
The errors I get are either <Error></Error> or The server encountered an error and could not complete your request.
Is there any chance that there exists some other solution?
Has anyone encoutered such problem before?
I would be greatful for any help.
You aren't supplying quotes around the value of ID like you are for Name. That is, you need
'<Data Token="someToken" Name="'"$appName"'" ...>'
^^^
|||
||+- shell quote to protect $appName
|+- shell quote enclosing the XML
+- literal quote embedded in the XML
which results in the string (assuming appName=foo)
<Data Token="someToken" Name="foo" ...>