使用 bash 发送 json HTTP post

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7205927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:41:00  来源:igfitidea点击:

Send json HTTP post with bash

jsonbashpost

提问by rtacconi

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.

我想将一些数据发送到仅接受 json 对象的 Web 服务。我将使用 curl 来制作 HTTP POST,但我想知道有一个库可以在 bash 中创建一个 json 对象。

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

另一个要求是避免安装其他包(rpm/apt-get),而只安装其他 bash 文件作为库。

回答by rtacconi

This is an example taken form BigQuery API

这是从 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'

回答by CoolAJ86

Checkout TickTick.

结帐TickTick

It's a true Bash JSON parser.

这是一个真正的 Bash JSON 解析器。

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

很快就会有一个字符串化器,(但仅仅使用 bash 的 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"]``

回答by hewigovens

I recommand Jshon. http://kmkeen.com/jshon/

我推荐 Jshon。 http://kmkeen.com/jshon/

It is designed to be as usable as possible from within the shell and replaces fragile adhoc parsers made from grep/sed/awk as well as heavyweight one-line parsers made from perl/python.

它被设计为在 shell 中尽可能可用,并替换由 grep/sed/awk 制作的脆弱的临时解析器以及由 perl/python 制作的重量级单行解析器。

Requires Jansson,But you can build a static link version.

需要Jansson,但是你可以构建一个静态链接版本。