[英]Trying to make an API call to a remote endpoint with NestJS
var unirest = require("unirest");
var req = unirest("GET", "https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data");
req.query({
"ingr": "1 large apple"
});
req.headers({
"x-rapidapi-host": "HOST",
"x-rapidapi-key": "KEY",
"useQueryString": true
});
req.end(function (res) {
if (res.error) throw new Error(res.error);
console.log(res.body);
});
Im trying to make an API call with that doc and parameters in order to get a list of ingredients based on a search parameter.我正在尝试使用该文档和参数拨打 API 电话,以便根据搜索参数获取成分列表。
This is my service:这是我的服务:
async getAllNutrients(ingr: string) {
console.log(ingr);
const headersRequest = {
'x-rapidapi-host': 'edamam-edamam-nutrition-analysis.p.rapidapi.com',
'x-rapidapi-key': '5664b75c9fmsh66ac8e054422eb9p1600b8jsn878d097e8d2a',
useQueryString: true,
};
const result = await this.httpService.get(
`https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data` +
ingr,
{ headers: headersRequest },
);
console.log(result);
return result;
}
And this is my controller这是我的 controller
@Get('/list?:ingr')
getMacros(@Query('ingr') ingr) {
return this.macroService.getAllNutrients(ingr);
}
I tried to change QUery and Param but none are working.我试图更改 QUery 和 Param,但都不起作用。 On postman i make an API call like this: "localhost:3000/macros/list?ingr="1 large apple" And my 2 console.log returns:
在 postman 上,我像这样拨打 API 电话:“localhost:3000/macros/list?ingr="1 large apple" 我的 2 console.log 返回:
"1 large apple"
Observable { _isScalar: false, _subscribe: [Function] }
[Nest] 7460 - 2020-09-21 16:00:55 [ExceptionsHandler] Request failed with status code 404 +441782ms
Error: Request failed with status code 404
I tried to use pipe like this example:我试着像这个例子一样使用 pipe:
getQuote(id){
return this.http.get('http://quotesondesign.com/wp-json/posts/' + id)
.pipe(
map(response => response.data)
);
}
But the result was the same.但结果是一样的。 Any help?
有什么帮助吗?
Looks like your issue is in your controllers route.看起来你的问题出在你的控制器路线上。 Changing
@Get('/list?:ingr')
to @Get('/list')
should resolve this.将
@Get('/list?:ingr')
更改为@Get('/list')
应该可以解决此问题。 I believe passing ?:ingr
in the path is setting a param with key ingr
.我相信在路径中传递
?:ingr
是设置一个带有键ingr
的参数。
Queries do not need to be added to the route.查询不需要添加到路由中。 They are accessed using the
@Query
decorator.使用
@Query
装饰器访问它们。
In your service function为您服务 function
const result = await this.httpService.get(
`https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data` +
ingr,
{ headers: headersRequest },
);
with ingr
is 1 large apple
then the API URL will become "https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data 1 large apple ". ingr
是1 large apple
,然后 API URL 将变为“https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data 1 大苹果”。
I think this is an incorrect API URL, and you don't want to call the API like that.我认为这是不正确的 API URL,您不想那样调用 API。
Change it to改成
`https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data?ingr=${ingr}`,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.