Site icon R-bloggers

JSON Payload for POST Request

[This article was first published on R on datawookie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Starting with JSON body because this is the way that most API documentation will give you the payload examples.

body = '{
    "filters": {
        "keywords": ["money","government"],
        "award_type_codes": [
            "A",
            "B",
            "C",
            "D"
        ]
    },
    "fields": [
        "Award ID",
        "Mod",
        "Recipient Name",
        "Action Date",
        "Transaction Amount",
        "Awarding Agency",
        "Awarding Sub Agency",
        "Award Type"
    ],
    "page": 1,
    "limit": 35,
    "sort": "Transaction Amount",
    "order": "desc"
}'

library(httr)

Send the body as a JSON string.

POST("https://api.usaspending.gov//api/v2/search/spending_by_transaction/", content_type_json(), body = body)

Send the body as a JSON encoded list.

POST("https://api.usaspending.gov//api/v2/search/spending_by_transaction/", body = body_list, encode = "json")

To leave a comment for the author, please follow the link and comment on their blog: R on datawookie.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.