Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cURL to send JSON within a BASH script

Alright, here's what I'm trying to do. I'm attempting to write a quick build script in bash that will check out a private repository from GitHub on a remote server. To do this a "hands off" as possible, I want to generate a local RSA key set on the remote server and add the public key as a Deploy Key for that particular repository. I know how to do this using GitHub's API, but I'm having trouble building the JSON payload using Bash.

So far, I have this particular process included below:

#!/bin/bash

ssh-keygen -t rsa -N '' -f ~/.ssh/keyname -q
public_key=`cat ~/.ssh/keyname.pub`

curl -u 'username:password' -d '{"title":"Test Deploy Key", "key":"'$public_key'"}' -i https://api.github.com/repos/username/repository/keys

It's just not properly building the payload. I'm not an expert when it comes to string manipulation in Bash, so I could seriously use some assistance. Thanks!

like image 830
Wilhelm Murdoch Avatar asked Jan 17 '12 23:01

Wilhelm Murdoch


People also ask

How do I post JSON with Curl?

To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.

How do I pass a JSON variable in bash?

First, your name property is a string, so you need to add double quotes to it in your json. Second, using single quotes, bash won't do variable expansion: it won't replace $r_name with the variable content (see Expansion of variable inside single quotes in a command in bash shell script for more information).

How do you pass payload in Curl command?

To post data in the body of a request message using Curl, you need to pass the data to Curl using the -d or --data command line switch. The Content-Type header indicates the data type in the body of the request message.

Can we use Curl command in shell script?

The curl command transfers data to or from a network server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE). It is designed to work without user interaction, so it is ideal for use in a shell script.


1 Answers

It's not certain, but it may help to quote where you use public_key, i.e.

curl -u 'username:password' \
   -d '{"title":"Test Deploy Key", "key":"'"$public_key"'"}' \
   -i https://api.github.com/repos/username/repository/keys

Otherwise it will be much easier to debug if you use the shell's debugging options set -vx near the top of your bash script.

You'll see each line of code (or block (for, while, etc) as it is in your file. Then you see each line of code with the variables expanded to their values.

If you're still stuck, edit your post to show the expanded values of variables for the problem line in your script. What you have looks reasonable at first glance.

like image 176
shellter Avatar answered Oct 04 '22 14:10

shellter