Sign in | Sign up

Angular 5 /v2/natural/nutrients

Tagged:

Currently contains 5 posts

Avatar

April 18, 2018 15:03

ivanfantang

Hi try to use angular 5 to use this API but always get
{message: "child “query” fails because [“query” is required]", id: “0f7c6aef-f46d-4180-8715-99e4d6c03698”} error message
here is my code:
const httpOption = {
headers: new HttpHeaders({
‘Content-Type’: ‘applicaiton/json’,
‘x-app-id’:‘xxxxxxxxxx’,
‘x-app-key’:‘xxxxxxxxxxx’
})
}

getNutritionInfo(ingrident:string):Observable<any>{
let url = ‘https://trackapi.nutritionix.com/v2/natural/nutrients’;

let body={ ‘query’:“for breakfast i ate 2 eggs, bacon, and french toast”, ‘timezone’: “US/Eastern” } return this.http.post<any>(url,body, httpOption); }

Avatar

April 18, 2018 15:08

varungupta

Hello, please stringify your body into a JSON string rather than passing it as a Javascript object literal

Avatar

April 19, 2018 07:13

ivanfantang

Thank you for your feedback, but I update my code as below:

return this.http.post<any>(url,JSON.stringify({’query’:’apple’}), httpOption);

I still get error message as below:
error
:
{message: "child “query” fails because [“query” is required]", id: “77672f81-4dd5-40b3-9d0b-9f400304aad1”}

Avatar

April 19, 2018 07:18

ivanfantang

by the way, the same setting, it can get data through postman.

Body
{
“query”:“1/2 tsp Ginger”,
“timezone”: “US/Eastern”
}
with setting JSON

Avatar

April 20, 2018 00:18

ivanfantang

I just want to let everybody know, issue has been fixed.

One stupid mistake

‘Content-Type’: ‘applicaiton/json’,
typo applicaiton

Reply to thread