Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass several parameters through curl in `get` request?

I execute a request of this kind: curl http://127.0.0.1:8000/api/v1/landing?param1=1&param2=2 As a result, I get only the first parameter. Why is this happening and how to fix it?

like image 878
unknown Avatar asked Oct 24 '25 22:10

unknown


2 Answers

You must put the argument within quotes (double or single, depending on what you want and on what platform you use) so that the shell doesn't interpret the & letter.

Like this:

curl "http://127.0.0.1:8000/api/v1/landing?param1=1&param2=2"
like image 101
Daniel Stenberg Avatar answered Oct 27 '25 02:10

Daniel Stenberg


When you pass several parameters through curl in get request, the URL used in the curl get call must be within double-quotes.

Please Note: you need to pass the URL in double-quotes when using several parameters, however this is not the case when there is a single parameter.

Below are the reasons why :

  1. If you see your CURL command, you will notice that you are using & to pass multiple values to your request parameters in this GET call/request.

curl http://127.0.0.1:8000/api/v1/landing?param1=1&param2=2

  1. In the Linux/Unix environment, & has a pre-defined interpretation. It is used to run any command in the background. So if & is present after any text, then the text is interpreted as a command and & means to run this command in the background.

  2. Any text after & is treated as a new command. So your above Curl get request is interpreted by Linux as 2 separate commands:
    i. curl http://127.0.0.1:8000/api/v1/landing?param1=1&
    ii. param2=2

Solution : The solution to avoid this interpretation by Unix/Linux is to surround your url with double quotes "

curl "http://127.0.0.1:8000/api/v1/landing?param1=1&param2=2"

This will help you pass multiple parameter values to your curl get request.

like image 21
Rohit Agrawal Avatar answered Oct 27 '25 02:10

Rohit Agrawal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!