Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send json HTTP post with bash

Tags:

json

post

bash

I would like to send some data to a web service which accept only a json object. I will use curl to make the HTTP POST but I am wondering it there is a library to create a json object in bash.

Another requirement is to avoid installation of other packages (rpm/apt-get) but only other bash files as libraries.

like image 805
rtacconi Avatar asked Aug 26 '11 14:08

rtacconi


2 Answers

This is an example taken form BigQuery API

curl -H "Authorization: GoogleLogin auth=<<YOUR_TOKEN>>" \
-X POST \
-H "Content-type: application/json" \
-d '{"params":{"q":"select count(*) from [bigquery/samples/shakespeare];"},"method":"bigquery.query"}' \
'https://www.googleapis.com/rpc'
like image 111
rtacconi Avatar answered Sep 30 '22 13:09

rtacconi


Checkout TickTick.

It's a true Bash JSON parser.

A stringifier should be in the works shortly, (but it wouldn't be difficult to create your own just using bash's foreach).

#!/bin/bash
. /path/to/ticktick.sh

# File
DATA=`cat data.json`
# cURL
#DATA=`curl http://foobar3000.com/echo/request.json`

tickParse "$DATA"

echo ``pathname``
echo ``headers["user-agent"]``
like image 27
coolaj86 Avatar answered Sep 30 '22 15:09

coolaj86