简体   繁体   English

如何在 R 中使用 POST 请求成功搜索和 API?

[英]How do I successfully search and API using a POST request in R?

I have been trying to set up a search in the https://explorer.natureserve.org/api-docs/ API, however, I keep getting the following error:我一直在尝试在https://explorer.natureserve.org/api-docs/ API 中设置搜索,但是,我不断收到以下错误:

"JSON parse error: Cannot deserialize value of type `java.lang.Integer` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.Integer` from Object value (token `JsonToken.START_OBJECT`)"

I am not sure where the issue is, as I have very similar code in Python, but the search works fine there.我不确定问题出在哪里,因为我在 Python 中有非常相似的代码,但在那里搜索工作正常。

Here is my code:这是我的代码:

search_text = '{
"criteriaType" : "species",
"textCriteria" : [{"paramType" : "textSearch", "searchToken" : "Acanthomintha ilicifolia", "matchAgainst" : "allScientificNames", "operator" : "similarTo"}],
"statusCriteria" : [ ],
"locationCriteria" : [ ],
"pagingOptions" : {"page" : null, "recordsPerPage" : null},
"recordSubtypeCriteria" : [ ],
"modifiedSince" : null,
"locationOptions" : null,
"classificationOptions" : null,
"speciesTaxonomyCriteria" : [ ]}'

search_json = fromJSON(search_text)

searchResults = POST("https://explorer.natureserve.org/api/data/speciesSearch", body=search_json, encode="json")

contentACIL_raw = content(searchResults)

It can be very messy decoding/re-encoding JSON data.解码/重新编码 JSON 数据可能非常混乱。 It would be much better just to store the data in R as a list, then let the POST command take care of encoding to JSON.将数据作为列表存储在 R 中会更好,然后让 POST 命令负责对 JSON 的编码。 For example例如

search_data = list(
  criteriaType= "species",  
  textCriteria= list(list(paramType = "textSearch", searchToken = "Acanthomintha ilicifolia", matchAgainst = "allScientificNames", operator = "similarTo")),
  statusCriteria= character(),
  locationCriteria= character(),
  pagingOptions= list(page = NA, recordsPerPage = NA),
  recordSubtypeCriteria = NA,
  modifiedSince = NA,
  locationOptions = NA,
  classificationOptions = NA,
  speciesTaxonomyCriteria = character()
)

searchResults = httr::POST("https://explorer.natureserve.org/api/data/speciesSearch", body=search_data, encode="json")
httr::content(searchResults)

Note I had to change "quickSearch" to "textSearch" because "matchAgainst" didn't seem to be a valid option for "quickSearch".请注意,我必须将“quickSearch”更改为“textSearch”,因为“matchAgainst”似乎不是“quickSearch”的有效选项。

You can preview the encoded JSON format with您可以使用预览编码的 JSON 格式

jsonlite::toJSON(search_data, auto_unbox=TRUE)
# {"criteriaType":"species","textCriteria":[{"paramType":"textSearch",
# "searchToken":"Acanthomintha ilicifolia","matchAgainst":"allScientificNames",
# "operator":"similarTo"}],"statusCriteria":[],"locationCriteria":[],
# "pagingOptions":{"page":null,"recordsPerPage":null},"recordSubtypeCriteria":null,
# "modifiedSince":null,"locationOptions":null,"classificationOptions":null,
# "speciesTaxonomyCriteria":[]} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM