Search code examples
jsoncurlgetcurl-commandline

How to pass a JSON in url via cURL?


I want to do something like following:

curl localhost:8080/myapp/?params={"first_key":"I'm the first value","second_key":"the second value"}

This is working pretty normal when I am trying to access the page via browser, but it does not work via cURL. What am I doing wrong?


Solution

  • The issue is caused by cURL's "URL globbing parser" (see the docs):

    You can specify multiple URLs or parts of URLs by writing part sets within braces as in:

    http://{one,two,three}.com

    So your command is being expanded to:

    curl localhost:8080/myapp/?params="first_key":"I'm the first value"
    curl localhost:8080/myapp/?params="second_key":"the second value"
    

    You need to use the -g option (or --globoff):

    This option switches off the "URL globbing parser". When you set this option, you can specify URLs that contain the letters {}[] without having them being interpreted by curl itself.

    So:

    curl -g localhost:8080/myapp/?params={"first_key":"I'm the first value","second_key":"the second value"}
    

    Then, to preserve the double quotes, you need to wrap the URL in single quotes:

    curl -g 'localhost:8080/myapp/?params={"first_key":"I'\''m the first value","second_key":"the second value"}'