Sunday, 18 August 2019

HTTPie: Set data in a file as request payload field value


You can set the non-string payload using := operator.

address.json
{
    "street" : "Cho street",
    "city" : "Bangalore",
    "state" : "Karnataka",
    "country" : "India"
}

Example
http POST http://localhost:8080/public/aboutUs name="ABC Corp" establishedYear:=2019 address:=@address.json

As you see above example, establishedYear is of type integer, it is set using := operator, similarly address is set with the data from address.json file.

Above request is equivalent to below snippet.

POST /public/aboutUs HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 145
Content-Type: application/json
Host: localhost:8080
User-Agent: HTTPie/1.0.2

{
    "address": {
        "city": "Bangalore",
        "country": "India",
        "state": "Karnataka",
        "street": "Cho street"
    },
    "establishedYear": 2019,
    "name": "ABC Corp"
}


Let’s post some data to aboutUs API.

$http POST http://localhost:8080/public/aboutUs name="ABC Corp" establishedYear:=2019 address:=@address.json
HTTP/1.1 201 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Fri, 02 Aug 2019 14:28:13 GMT
Expires: 0
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "address": {
        "city": "Bangalore",
        "country": "India",
        "state": "Karnataka",
        "street": "Cho street"
    },
    "establishedYear": 2019,
    "name": "ABC Corp"
}

$
$
$http http://localhost:8080/public/aboutUs name="ABC Corp" establishedYear:=2019 address:=@address.json
HTTP/1.1 201 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Fri, 02 Aug 2019 14:28:22 GMT
Expires: 0
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "address": {
        "city": "Bangalore",
        "country": "India",
        "state": "Karnataka",
        "street": "Cho street"
    },
    "establishedYear": 2019,
    "name": "ABC Corp"
}




Previous                                                    Next                                                    Home

No comments:

Post a Comment