How to encode spaces in curl GET request URL on Linux?

The problem is like this: I would like to send a GET request using curl and the content has spaces, such as

curl "http://example.com/send?msg=hello world"

The space and the “world” will be left away if this command is executed directory on Linux.

How to encode spaces in curl request URL?

You can use the --date-urlencode option of curl:

--data-urlencode <data>

(HTTP) This posts data, similar to the other --data options with
the exception that this performs URL-encoding. (Added in 7.18.0)
To be CGI-compliant, the <data> part should begin with a name 
followed by a separator and a content specification. The <data> 
part can be passed to curl using one of the following syntaxes:

content
This will make curl URL-encode the content and pass that on. 
Just be careful so that the content doesn't contain any = or 
@ symbols, as that will then make the syntax match one of the 
other cases below!

=content
This will make curl URL-encode the content and pass that on. 
The preceding = symbol is not included in the data.

name=content
This will make curl URL-encode the content part and pass that 
on. Note that the name part is expected to be URL-encoded 
already.

@filename
This will make curl load data from the given file (including 
any newlines), URL-encode that data and pass it on in the POST.

name@filename
This will make curl load data from the given file (including any
newlines), URL-encode that data and pass it on in the POST. The 
name part gets an equal sign appended, resulting in 
name=urlencoded-file-content. Note that the name is expected to 
be URL-encoded already.

You command could be:

curl -G "http://example.com/send" --data-urlencode "msg=hello world"

Here, -G is to tell curl that the data are used in an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a ‘?’ separator.

Similar Posts

  • SetProxy: 一个设置IE代理的命令行小工具

    IE的代理设置用起来并不方便,我自己而言就要经常更改代理服务器,相信很多人有跟我相同的需要。使用C++编写了一个小程序SetProxy调用Win32 API来设置代理服务器IP,用起来方便多了。 编译后为一个可运行console程序,将它放在\windows\system32目录下,使用时只要运行 SetProxy IP:port 就可以把IP设置好,如果要关闭代理,只需运行 SetProxy “” 命令行中运行,界面较土,但用着方便。可以考虑设置几个快捷方式在桌面或者工具栏,点击即设置代理,其它方式发挥想象。 程序下载地址. 源代码也放在这里,希望有需要的人可以用得上 ;) 源代码下载地址. 这是一份有些年头的代码, 如果你在较新的编译器下编译这个项目, 请先做[[setproxy-一个设置ie代理的小工具#comment-995|这里所述的小修改]]. — Eric on Apr. 9, 2014. Read more: Making Emacs Start Up Faster Profiling Vim to Find Out Which Plugin Makes Vim Slow Handling Sparse Files on Linux Spring Shell Technology For Java development Vim Tutorial for Beginners: vimtutor…

  • Bash comparison operators

    What are all the bash supported comparison operators? The man 1 test contains all the operators supported in bash. An omitted EXPRESSION defaults to false. Otherwise, EXPRESSION is true or false and sets exit status. It is one of: ( EXPRESSION ) EXPRESSION is true ! EXPRESSION EXPRESSION is false EXPRESSION1 -a EXPRESSION2 both EXPRESSION1…

  • Latex is stuck with a strange problem, see more information for details.

    $ make pdflatex main.tex This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian) (preloaded format=pdflatex) restricted write18 enabled. entering extended mode (./main.tex LaTeX2e <2016/02/01> Babel <3.9q> and hyphenation patterns for 81 language(s) loaded. (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2014/09/29 v1.4h Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo)) (./usenix.sty (/usr/share/texlive/texmf-dist/tex/latex/psnfss/mathptmx.sty)) (/usr/share/texlive/texmf-dist/tex/latex/graphics/epsfig.sty (/usr/share/texlive/texmf-dist/tex/latex/graphics/graphicx.sty (/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty) (/usr/share/texlive/texmf-dist/tex/latex/graphics/graphics.sty (/usr/share/texlive/texmf-dist/tex/latex/graphics/trig.sty) (/usr/share/texlive/texmf-dist/tex/latex/latexconfig/graphics.cfg) (/usr/share/texlive/texmf-dist/tex/latex/pdftex-def/pdftex.def (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/infwarerr.sty) (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ltxcmds.sty))))) (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifpdf.sty)…

  • How to detect whether a file is readable and writable in Python?

    Before reading or writing a file, access should be checked first. How to detect whether a file is readable and writable in Python? You can use the os.access(path, mode) library function https://docs.python.org/release/2.6.6/library/os.html#os.access like the Linux access library function for C. It returns True if access is allowed, False if not. For readable and writable, you…

2 Comments

  1. I do this as well and people told me you can not use -G (get) and I verified it does indeed do a get vs a post.

Leave a Reply

Your email address will not be published. Required fields are marked *