R httr发送的IPaC位置API的POST请求

huangapple go评论69阅读模式
英文:

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"))
 $ 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.

英文:

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.

答案1

得分: 0

以下是翻译好的部分:

"Turns out the solution was pretty straightforward with a WKT parameter:" 译为 "原来解决方案相当简单,只需使用WKT参数:"

"url = 'https://ipacb.ecosphere.fws.gov/location/api/resources'" 译为 "url = 'https://ipacb.ecosphere.fws.gov/location/api/resources'"

"body <- '{\n "projectLocationWKT": "Polygon((-95.1306152 30.4486737,-93.6584473 29.4061051,-94.6691895 28.5314486,-96.5368652 29.9834867,-95.1306152 30.4486737))",\n "timeout": 2,\n "apiVersion": "1.0.0",\n "includeOtherFwsResources": true,\n "includeCrithabGeometry": false\n}'" 译为 "body <- '{\n "projectLocationWKT": "Polygon((-95.1306152 30.4486737,-93.6584473 29.4061051,-94.6691895 28.5314486,-96.5368652 29.9834867,-95.1306152 30.4486737))",\n "timeout": 2,\n "apiVersion": "1.0.0",\n "includeOtherFwsResources": true,\n "includeCrithabGeometry": false\n}'"

"r <- httr::POST(url, content_type_json(), body = body, encode = "raw")" 译为 "r <- httr::POST(url, content_type_json(), body = body, encode = "raw")"

"str(content(r, "parsed"))" 译为 "str(content(r, "parsed"))"

英文:

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

url = &#39;https://ipacb.ecosphere.fws.gov/location/api/resources&#39;
body &lt;- &#39;{
  &quot;projectLocationWKT&quot;:  &quot;Polygon((-95.1306152 30.4486737,-93.6584473 29.4061051,-94.6691895 28.5314486,-96.5368652 29.9834867,-95.1306152 30.4486737))&quot;,
  &quot;timeout&quot;: 2,
  &quot;apiVersion&quot;: &quot;1.0.0&quot;,
  &quot;includeOtherFwsResources&quot;: true,
  &quot;includeCrithabGeometry&quot;: false
}&#39;

r &lt;- httr::POST(url, content_type_json(), body = body, encode = &quot;raw&quot;)
str(content(r, &quot;parsed&quot;))

huangapple
  • 本文由 发表于 2023年5月11日 08:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定