0

I'm a curl and JSON beginner.

I am trying to use an online API and this works:

curl -X POST https://blah.com/trigger/{test_event}/with/key/mykeynumber123

The service specifies that I should also be able to send additional data like so:

You can also send an optional JSON body of:

{ "value1" : "", "value2" : "", "value3" : "" }

The data is completely optional, and you can also pass value1, value2, and value3 as query parameters or form variables. This content will be passed on to the Action in your Recipe.

So my issue is I don't know how to format this. The first curl example works but if I try this for example it won't work:

curl -X POST https://blah.com/trigger/{test-event}{"value1":"test","value2":"test2","value3":"test3"}/with/key/mykeynumber

Any advice?

Vesa
  • 197
  • 1
  • 7
  • See http://stackoverflow.com/questions/15425446/how-to-put-a-json-object-with-an-array-using-curl/15434415#15434415 for a very very similar question/answer, only using PUT instead of POST but the rest is the same as in your question. – Daniel Stenberg Oct 08 '15 at 11:11

2 Answers2

1

You can post json data with appropriate header:

curl -X POST
  https://blah.com/trigger/{test_event}/with/key/mykeynumber123
  -H "Content-Type: application/json"
  -d '{ "value1" : "", "value2" : "", "value3" : "" }'

If you are running this from windows, then use double quote (") instead of single at -d parameter.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • This works. Do you know how I can send a variable such as $myvar as value1? – Vesa Oct 08 '15 at 13:55
  • Or file content? It would be nice if I could send the content of a text file using this method. – Vesa Oct 08 '15 at 14:06
0

Try

curl --data "value1=test&value2=test2&value3=test3" https://blah.com/trigger/{test_event}/with/key/mykeynumber123

I think the JSON is just a way of formatting it for display purposes.

Your next sentence mentions passing them as form parameters, which is what the above command does.

The query parameters mentioned would be something like:

curl -X POST https://blah.com/trigger/{test_event}/with/key/mykeynumber123?value1=test&value2=test2&value3=test3
ronalchn
  • 12,225
  • 10
  • 51
  • 61
  • It almost works. The first value1 is sent but the other two were ignored. curl -X POST https://blah.com/trigger/{test_event}/with/key/mykeynumber123?value1=test&value2=test2&value3=test3 – Vesa Oct 08 '15 at 11:56
  • 1. you might need to url encode the parameters, or 2. there is a maximum URL length (in which case you need to choose the first method) – ronalchn Oct 08 '15 at 11:59
  • I don't know how to do that. Also, can I send a variable and if so how? – Vesa Oct 08 '15 at 12:05