Search code examples
rjsonhttr

R httr POST request for IPaC Location API


I'm attempting to run the example request for the IPaC location API from: https://ipac.ecosphere.fws.gov/location/api

What I have below returns status 406:

library(httr)
library(jsonlite)

url <- 'https://ipacb.ecosphere.fws.gov/location/api/resources/'
body <- '{
    "location.footprint": {
        \"coordinates\": [[[[-95.1306152,30.4486737],[-93.6584473,29.4061051],
        [-94.6691895,28.5314486],[-96.5368652,29.9834867],
        [-95.1306152,30.4486737]]]],
        \"type\":\"Polygon\"
    },
    "timeout": 2,
    "apiVersion": "1.0.0",
    "includeOtherFwsResources": true,
    "includeCrithabGeometry": false
}'

r <- httr::POST(url, body = body, encode = "json")
# r <- httr::POST(url, content_type_json(), body = body)
# r <- httr::POST(url, body = body)
# r <- httr::POST(url, body = body, encode = "raw")
# r <- httr::POST(url, content_type_json(), body = body, encode = "raw")
str(content(r, "parsed"))
List of 5
 $ timestamp: num 1.68e+12
 $ status   : int 406
 $ error    : chr "Not Acceptable"
 $ message  : chr "A location was not specified. Locations can be sent as JSON in the location.footprint parameter or as WKT as th"| __truncated__
 $ path     : chr "/location/api/resources/"

I've adjusted the formatting for the location.footprint as well as trying different permutations of httr::POST (commented out in code above) but I'm not understanding how to translate the location.footprint in my body to a format httr will accept. The example request I'm running works fine and returns 200 in Postman.


Solution

  • Turns out the solution was pretty straightforward with a WKT parameter:

    url = 'https://ipacb.ecosphere.fws.gov/location/api/resources'
    body <- '{
      "projectLocationWKT":  "Polygon((-95.1306152 30.4486737,-93.6584473 29.4061051,-94.6691895 28.5314486,-96.5368652 29.9834867,-95.1306152 30.4486737))",
      "timeout": 2,
      "apiVersion": "1.0.0",
      "includeOtherFwsResources": true,
      "includeCrithabGeometry": false
    }'
    
    r <- httr::POST(url, content_type_json(), body = body, encode = "raw")
    str(content(r, "parsed"))