How to use CURL with a JSON payload and the Trello API?
Asked by August Mohr:

I'm using CURL to send GET/PUT/POST requests to the Trello API. Up until now I've been following the format of the calls in the Trello API Introduction \<<https://developer.atlassian.com/cloud/trello/guides/rest-api/api-introduction>\> where all the data is on the CURL command line.  

But I recently saw an answer in the Trello forum that referred to a JSON payload. Searching for info on CURL payloads got me this page on Stack Exchange: <https://stackoverflow.com/questions/18611903/how-to-pass-payload-via-json-file-for-curl>

On that page is the caveat "that will only work if the server accepts json input".  

So, does the server accept json input? Can I send a CURL command to the Trello API that uses JSON-formatted input rather than having to put all input values on the command line?

Thanks


Answers:
author: Michael Pryor
body: Yes although I think the authentication parameters (token etc) still need to be part of query string.   

see the PUT examples here: <https://developer.atlassian.com/cloud/trello/guides/rest-api/api-introduction/>

comments: author: August Mohr
body: Thanks. That's the page I linked to in my question. I've been following that format.  

So far, in my shell script, I have a construction like this:

```
? ? ? ? curl -s \
? ? ? ? --request POST \
? ? ? ? --url "https://api.trello.com/1/cards\
?$WhoMe\
&idList=$ToDoTodayListID\
&name=$NewCardName\
&pos=$PossiblePos\
"? ? ? |
? ? ? ? jq -r ' . | .name, .pos '
```

In the above $WhoMe contains my token and key.  

The idList, name, and pos data are all provided on the curl command line and the response is piped through jq to display only the name and pos of the created card, as confirmation.

What I'm interested in is being able to have a more complex creation command read from a file or from a shell variable that is in JSON format. I believe that I have to add this to the curl options to have it properly upload JSON data:

    --header "Content-Type: application/json"?

Your answer is, briefly, yes, that ought to work. But I'm looking for more specifics about how to actually construct the curl command.  

For instance, the sample commands on the API Introduction page that we both cited have constructions like this:

    curl 'https://api.trello.com/1/members/me/boards?key={yourKey}&token={yourToken}'

But when I inserted $MyKey and $MyToken values into that string, I got an error from the shell because the single quotes had to be changed to double quotes so that the variables will be expanded to their values. That was a hard glitch to debug because it was hard to determine where, among several levels, the error was actually occurring and I'm pretty new at all this.  

So I'm pushing back at answers that are of the form "do it in the usual way" because I don't know what that "usual way" is.  

How do I get curl to send the data in a way that the Trello server will accept? I'd like the option of the data being either in a file or in a shell variable.  

Thanks for your time and the tutoring.


author: August Mohr
body: I'm still looking for how to do this. My original questions was "How" and Michaels's answer was "Yes, it's possible".  

I suspect it may be a specific usage of a specific curl option, and if you know how it works already, the answer is obvious in the curl manual. Maybe I need someone to tell me, "This means this and that means do this and it will be interpreted as that, which is what you need."

But I don't know how it works already, so I'm still looking. I found a reference in Stack Overflow to the content type header flag, but that is the limit of my understanding. I need that flag. Then what? I know where to put the flag, but where does the JSON go?


author: Michael Pryor
body: The answer with 4300 upvotes here should help :)

<https://stackoverflow.com/questions/7172784/how-do-i-post-json-data-with-curl>


Tags: api

curl

json

rest-api