typescript - Axios 查询参数?
在使用 Postman 时,我只需输入查询参数,但在 axios 中我无法做到。我必须使用字符串拼接。这很糟吗?
const url = "https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/ImageSearchAPI?pageNumber="+data.pageNumber+"&pageSize="+data.pageSize+"&q="+data.q+"&autoCorrect=true"
const result = await axios.default.get(url,headers)
const values = result.data.value
for (const value of values) {
const imageURL = value.url as string
res.images.push(imageURL)
}
response.send(res)
谢谢。我觉得 axios 真难用……它有 1300 万次下载,也许我用的是大锤,而我只需要一个水龙头。我应该换成像 unirest 这样更轻量的 HTTP 服务吗?
答案#1
axios.get() 的第二个参数是 Axios 选项:它会序列化 options.params 并将其添加到查询字符串中。尝试以下操作:
const res = await axios.get('https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/ImageSearchAPI', { params: {pageNumber:data.pageNumber,pageSize:data.pageSize,q:data.q,autoCorrect:true},headers);
