Search code examples
curlgetcurly-braces

curl GET send JSON as HTTP parameter


I'm trying to send a message to AWS SQS from bash script using curl. I took this article as a base. As a result I'm building the following request: https://sqs.eu-west-1.amazonaws.com/[MY_QUEUE_NAME]?Action=SendMessage&MessageBody=[MY_MESSAGE]

And here starts the most complicated. Whenever I'm using a plain text as MY_MESSAGE everything works perfect and message is delivered to SQS, but I want to send a JSON: {"mahine": "SOME_MACHINE", "user": "SOME_USER"}

I haven't found a way to use curly braces in GET request as HTTP parameter. How can I make {} symbols be recognized as simple text?


Solution

  • Really you should be adding it to the request body in a POST with something like this:

    curl -H "Content-Type: application/json" \
         -X POST \
         -d '{"machine":"SOME_MACHINE","user":"SOME_USER"}' \
         https://sqs.eu-west-1.amazonaws.com/[MY_QUEUE_NAME]?Action=SendMessage
    

    You can however add JSON to a GET request in the URL but you will need to encode it first using percent escaping (i.e. %20 escapes a space). { and } are not valid in a URL and need to be encoded to %7B and %7D receptively.

    You can use this URL encoder to encode your JSON to an acceptable format for the URL.