javascript - 如何在 JavaScript 中向 ContextualWeb 图像搜索 API 发出 GET 请求?
问题描述
我正在使用RapidAPI访问ContextualWeb图像搜索 API。给出的代码示例仅适用于服务器端: https ://rapidapi.com/contextualwebsearch/api/web-search/
我正在尝试将 API 集成到 React JS 应用程序中。如何在JavaScript中发出 GET 请求?
下面是来自 Rapid API 网站的CURL示例:
curl --get --include 'https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/ImageSearchAPI?autoCorrect=false&pageNumber=1&pageSize=10&q=Taylor+Swift&safeSearch=false' \
-H 'X-RapidAPI-Host: contextualwebsearch-websearch-v1.p.rapidapi.com' \
-H 'X-RapidAPI-Key: XXXXXXXX'
图像搜索 API 应返回结果 JSON。
解决方案
给定的curl
请求转换为后续fetch API
(由浏览器实现)请求。
const url ="https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/ImageSearchAPI?autoCorrect=false&pageNumber=1&pageSize=10&q=Taylor+Swift&safeSearch=false"
const options = {
method: 'GET',
headers: {
"X-RapidAPI-Host": "contextualwebsearch-websearch-v1.p.rapidapi.com",
"X-RapidAPI-Key": "XXXXXXXX"
},
}
fetch(url, options)
.then(response => response.json())
.then(data => console.log(data))
.catch(e => console.error(e))
推荐阅读
- mirth - Mirth - 如何在有多个目的地时处理响应
- flutter - 如何通过引用有状态小部件来传递数据?我想更改有状态小部件内的变量并更新原始变量
- python - 来自不同位置的 2 个输入 python 多线程
- sqlite - 运行 Sqlite 批处理文件以读取同一目录中的所有 .txt 文件
- python - 清理时出现python多进程socket.error
- javascript - 0 在 ES6 中被认为是真的吗?
- file - 从外部访问共享首选项/将值存储到新文件中并从外部访问它
- javascript - 有没有办法用代码“清除网站数据”浏览器?
- javascript - 在Javascript中,有没有一种多态设置器的方法
- visual-studio - Visual Studio 从另一个文件打开类的文件