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

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

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:

  1. library(httr)
  2. library(jsonlite)
  3. url <- 'https://ipacb.ecosphere.fws.gov/location/api/resources/'
  4. body <- '{
  5. "location.footprint": {
  6. "coordinates": [[[[-95.1306152,30.4486737],[-93.6584473,29.4061051],
  7. [-94.6691895,28.5314486],[-96.5368652,29.9834867],
  8. [-95.1306152,30.4486737]]]],
  9. "type":"Polygon"
  10. },
  11. "timeout": 2,
  12. "apiVersion": "1.0.0",
  13. "includeOtherFwsResources": true,
  14. "includeCrithabGeometry": false
  15. }'
  16. r <- httr::POST(url, body = body, encode = "json")
  17. # r <- httr::POST(url, content_type_json(), body = body)
  18. # r <- httr::POST(url, body = body)
  19. # r <- httr::POST(url, body = body, encode = "raw")
  20. # r <- httr::POST(url, content_type_json(), body = body, encode = "raw")
  21. str(content(r, "parsed"))
  1. $ timestamp: num 1.68e+12
  2. $ status : int 406
  3. $ error : chr "Not Acceptable"
  4. $ message : chr "A location was not specified. Locations can be sent as JSON in the location.footprint parameter or as WKT as th"| __truncated__
  5. $ 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:

  1. library(httr)
  2. library(jsonlite)
  3. url <- 'https://ipacb.ecosphere.fws.gov/location/api/resources/'
  4. body <- '{
  5. "location.footprint": {
  6. \"coordinates\": [[[[-95.1306152,30.4486737],[-93.6584473,29.4061051],
  7. [-94.6691895,28.5314486],[-96.5368652,29.9834867],
  8. [-95.1306152,30.4486737]]]],
  9. \"type\":\"Polygon\"
  10. },
  11. "timeout": 2,
  12. "apiVersion": "1.0.0",
  13. "includeOtherFwsResources": true,
  14. "includeCrithabGeometry": false
  15. }'
  16. r <- httr::POST(url, body = body, encode = "json")
  17. # r <- httr::POST(url, content_type_json(), body = body)
  18. # r <- httr::POST(url, body = body)
  19. # r <- httr::POST(url, body = body, encode = "raw")
  20. # r <- httr::POST(url, content_type_json(), body = body, encode = "raw")
  21. str(content(r, "parsed"))
  1. List of 5
  2. $ timestamp: num 1.68e+12
  3. $ status : int 406
  4. $ error : chr "Not Acceptable"
  5. $ message : chr "A location was not specified. Locations can be sent as JSON in the location.footprint parameter or as WKT as th"| __truncated__
  6. $ 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:

  1. url = &#39;https://ipacb.ecosphere.fws.gov/location/api/resources&#39;
  2. body &lt;- &#39;{
  3. &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;,
  4. &quot;timeout&quot;: 2,
  5. &quot;apiVersion&quot;: &quot;1.0.0&quot;,
  6. &quot;includeOtherFwsResources&quot;: true,
  7. &quot;includeCrithabGeometry&quot;: false
  8. }&#39;
  9. r &lt;- httr::POST(url, content_type_json(), body = body, encode = &quot;raw&quot;)
  10. 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:

确定