1

I know I can use

curl --data "param1=value1&param2=value2" http://hostname/resource

or

curl --request POST https://$url?param1=value1&param2=value2

But what do I need to do if param1 is value and param2 is a JSON?

It just does not work(tm) if I just toss the JSON in there, even using a variable

$json='{"data":"value"}'
curl --request POST https://$url?param1=value1&param2=$json

What is the trick here? Note that I HAVE TO make only one call.

Thank you!

1
  • Can you be a bit more specific about what it is that does not work? Do you get an error? Does the data not arrive at the server? Commented Sep 27, 2014 at 15:13

1 Answer 1

1

Ok, if we escape everything (using python) here's what it looks like

>>> x
'{"data": "value"}'
>>> urllib.urlencode({'param1':'value1', 'param2':x})
'param2=%7B%22data%22%3A+%22value%22%7D&param1=value1'

Or, using the curl option

curl localhost:8080 --data-urlencode 'param1={"data":"value"}'

Will send to the server

param1=%7B%22data%22%3A%22value%22%7D

You may notice that the first version has a +, which probably comes from the space in the json encoded, not sure it works or if it can be removed

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the trick is to url-encode the whole JSON. I am using another way to do that but your idea is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.