Skip to content

Bash shell functions that wrap curl to provide a convenient way to send restful requests

License

Notifications You must be signed in to change notification settings

iintothewind/curl-bash-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

curl-bash-wrapper

curl-bash-wrapper is a set of useful shell functions that wrap curl command to create restful requests in an easy way.

prerequisites

  • curl command
  • python
  • some environment variables (optional)
export REQ_BASE="https://localhost/base/url"
export REQ_USER="usr"
export REQ_PWD="pwd"
export REQ_METHOD="POST"

usage

first things first, we need to load functions by using source curl-bash-wrapper.sh

send GET request

cf_req -l 'http://api.openweathermap.org/data/2.5/weather?appid=12345&lang=zh_cn&q=Shanghai' -m get

response should be:

{"weather":[{"id":701,"main":"Mist","description":"薄雾","icon":"50n"}],"base":"stations","main":{"temp":287.59,"pressure":1022,"humidity":55,"temp_min":286.15,"temp_max":289.15},"visibility":10000,"wind":{"speed":3,"deg":110},"clouds":{"all":0},"dt":1521896400,"sys":{"type":1,"id":7452,"message":0.0081,"country":"CN","sunrise":1521841965,"sunset":1521886091},"id":1236,"name":"Shanghai","cod":20}

After we exported some environment variables:

export REQ_BASE="http://api.openweathermap.org"
export REQ_METHOD="GET"

The command can be simplified to:

cf_req -l '/data/2.5/weather?appid=12345&lang=zh_cn&q=Shanghai'

send POST request

Assume we are using a neo4j database, and exported some environment variables:

export REQ_BASE="http://localhost:7474/db/data/cypher"
export REQ_USER="neo4j"
export REQ_PWD="admin"
export REQ_METHOD="POST"

The command can be simplified to:

cf_req -d '{ "query" : "MATCH (ee:Person) WHERE ee.name = \"Emil\" RETURN ee;", "params" : {} }'

Or use an EOF for free text typing:

$ cf_req <<EOF
> {
>   "query": "match (a)--(b) return a,b",
>   "params": {
>   }
> }
> EOF

Or even we can store json request body into a query.json file, then:

cat query.json | cf_req

format json response

In order to format json response, We can connect cf_req and cf_jsonfmt with a pipe:

cf_req -l '/data/2.5/weather?appid=a38e&lang=zh_cn&q=Shanghai' | cf_jsonfmt

The response will be formatted to:

{
    "base": "stations",
    "id": 17236,
    "main": {
        "humidity": 55,
        "pressure": 1022,
        "temp": 287.59,
        "temp_max": 289.15,
        "temp_min": 286.15
    },
    "name": "Shanghai",
    "weather": [
        {
            "description": "\u8584\u96fe",
            "icon": "50n",
            "id": 701,
            "main": "Mist"
        }
    ],
    "wind": {
        "deg": 110,
        "speed": 3
    }
}

pick value from json response

We can use cf_parse to pipe after any json response, and use key as the first parameter of this function:

cf_req -l '/data/2.5/weather?appid=a38e&lang=zh_cn&q=Shanghai' | cf_jsonfmt | cf_parse '["weather"][0]["main"]'

result:

Mist

About

Bash shell functions that wrap curl to provide a convenient way to send restful requests

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages