Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Created October 21, 2020 10:34
Show Gist options
  • Save angelabauer/dd71d7072626afd728e1730584c6e4b8 to your computer and use it in GitHub Desktop.
Save angelabauer/dd71d7072626afd728e1730584c6e4b8 to your computer and use it in GitHub Desktop.
import requests
GENDER = YOUR GENDER
WEIGHT_KG = YOUR WEIGHT
HEIGHT_CM = YOUR HEIGHT
AGE = YOUR AGE
APP_ID = YOUR APP ID
API_KEY = YOUR API KEY
exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
exercise_text = input("Tell me which exercises you did: ")
headers = {
"x-app-id": APP_ID,
"x-app-key": API_KEY,
}
parameters = {
"query": exercise_text,
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT_CM,
"age": AGE
}
response = requests.post(exercise_endpoint, json=parameters, headers=headers)
result = response.json()
print(result)
@karuna-karan
Copy link

Tell me Which exercise you did: ran 3miles and walked 2km. {'message': 'child "query" fails because ["query" is required]', 'id': '95593e38-789e-4cee-8627-15d9193afdac'} This message i received anytime i ran this code , can any one explain it to me

I have the same issue, fix it by map the parameters with json, like json: parameters, but not params:parameters(which is used in requests.get method.

i have facing ht same issue please resolve the issue.

@kriswen
Copy link

kriswen commented Apr 4, 2024

Tell me Which exercise you did: ran 3miles and walked 2km. {'message': 'child "query" fails because ["query" is required]', 'id': '95593e38-789e-4cee-8627-15d9193afdac'} This message i received anytime i ran this code , can any one explain it to me

I have the same issue, fix it by map the parameters with json, like json: parameters, but not params:parameters(which is used in requests.get method.

i have facing ht same issue please resolve the issue.

for requests.post() method you have to use json as the parameter
see here: https://www.w3schools.com/python/ref_requests_post.asp

@darrnih
Copy link

darrnih commented Apr 8, 2024

Doesn't work for me, i keep getting {"message":"invalid app id/key","id":"39386a26-a250-446b-b58b-c2d38fd78932"} or a variation of the same invalid app id/key message

@kriswen
Copy link

kriswen commented Apr 8, 2024

share your code

@stevenson007
Copy link

Everything was perfect. No errors for me.

@Dabro1203
Copy link

I got mine working eventually - persistence and resilience :)

Here is my code below. I have put a few pointers in brackets in my code to highlight the areas were I was getting problems.

Hope this is of some help :)

import requests

exercise = input("Please tell us what exercise you have done?")
APP_ID = "xxxxxxxxx" (Make sure your APP_ID is submitted as a string)
APP_KEY = "xxxxxxxxxxxxxxxxxxxxxxxx " (Make sure the APP_KEY is a string and a carbon copy of what appears in your account, just take off the final -, but leave the dead space at the end)
API_ENDPOINT = "https://trackapi.nutritionix.com/v2/natural/exercise"

headers = {
"x-app-id": APP_ID,
"x-app-key": APP_KEY
}

parameters = {
"query": exercise,
"gender": "male", (Please note the gender is a string but the weight, height and age are all integer values)
"weight_kg": 56,
"height_cm": 120,
"age": 35
}

response = requests.post(API_ENDPOINT, headers= headers, json= parameters)
print(response)
result = response.json()
print(result)

@john-sc
Copy link

john-sc commented May 29, 2024

"Weight Kg not allowed"
Check your endpoint.
It should be v2/natural/exercise
You have v2/natural/nutrients and for that endpoint Weight Kg is not a valid parameter.

@AbiollaghJames
Copy link

AbiollaghJames commented Jun 12, 2024

That documentation is so hard to understand, couldn't find query parameters for some reason.

@Ash-Rich38
Copy link

That documentation is so hard to understand, couldn't find query parameters for some reason.

It's there it's just tucked under and expand menu which was not the easiest to find.

@Harsha0130
Copy link

Tell me Which exercise you did: ran 3miles and walked 2km. {'message': 'child "query" fails because ["query" is required]', 'id': '95593e38-789e-4cee-8627-15d9193afdac'} This message i received anytime i ran this code , can any one explain it to me

Can you share your cade, So that i can help you to solve the problem

@Simonmmr
Copy link

Simonmmr commented Jul 16, 2024

Hi I cant find the documentation that takes weight height and age for the parameters there is just query in the parameters list

Yeah I could not find other parameters too, plus I'm first time facing an api doc like this.

check please the API endpoint, first I also found it as an error and later my API endpoint is different from the one we should use https://trackapi.nutritionix.com/v2/natural/exercise

@MasterYodaPresents
Copy link

I don't work in cm/kg...I work in lbs and in...I converted our first exercise and bmi calculator to lbs and in...sorry...kg and cm are useless to me....can you please tell me how to do that here....Please and thank you!

@MasterYodaPresents
Copy link

I think I found the answer....thank you

@Vigilante120
Copy link

import requests

NUTRITION_API_KEY = "API KEY"
NUTRITION_NATURAL_ENDPOINT = "https://trackapi.nutritionix.com/v2/natural/exercise"
APP_ID = "APP ID"

headers = {
'Content-Type': 'application/json',
"x-app-id": f"{APP_ID}",
"x-app-key": f"{NUTRITION_API_KEY}"
}
parameters = {
"query": input("What Exercise You Just Did: ")
}

nutrition = requests.post(url=NUTRITION_NATURAL_ENDPOINT, json=parameters, headers=headers)
print(nutrition.json())

OUTPUT:
WhatExerciseYouJustDid: Cricket{
'exercises': [
{
'tag_id': 215,
'user_input': 'cricket',
'duration_min': 30,
'met': 4.8,
'nf_calories': 168,
'photo': {
'highres': 'https://d2xdmhkmkbyw75.cloudfront.net/exercise/215_highres.jpg',
'thumb': 'https://d2xdmhkmkbyw75.cloudfront.net/exercise/215_thumb.jpg',
'is_user_uploaded': False
},
'compendium_code': 15150,
'name': 'cricket',
'description': None,
'benefits': None
}
]
}

@Shlok1613
Copy link

`import requests

APP_ID = "7bceec16"
APP_KEY = "844902aaedb6f200acca45f3e3364288"
GENDER = "Male"
AGE = "20"
WEIGHT_KG = "65"
HEIGHT_CM = "175"

exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"

exercise_text = input("Tell me which exercise did you do? ")
headers = {
"x-app-id": APP_ID,
"x-app-key": APP_KEY,
"Content-Type": "application/json"
}
parameters = {
"query": exercise_text,
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT_CM,
"age": AGE
}

response = requests.get(url=exercise_endpoint, json=parameters, headers=headers)
response.raise_for_status()

tried this code a lot made many changes and yet getting this output

OUTPUT
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://trackapi.nutritionix.com/v2/natural/exercise`

@ares77717
Copy link

`import requests

APP_ID = "7bceec16" APP_KEY = "844902aaedb6f200acca45f3e3364288" GENDER = "Male" AGE = "20" WEIGHT_KG = "65" HEIGHT_CM = "175"

exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"

exercise_text = input("Tell me which exercise did you do? ") headers = { "x-app-id": APP_ID, "x-app-key": APP_KEY, "Content-Type": "application/json" } parameters = { "query": exercise_text, "gender": GENDER, "weight_kg": WEIGHT_KG, "height_cm": HEIGHT_CM, "age": AGE }

response = requests.get(url=exercise_endpoint, json=parameters, headers=headers) response.raise_for_status()

tried this code a lot made many changes and yet getting this output

OUTPUT raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://trackapi.nutritionix.com/v2/natural/exercise`

use GENDER = 'male'
AGE = 20 int not str
weight_kg = 65 same int not str
height_cm = 175
then save response in new variable data or results
data = response.json()
print(data)

@Shlok1613
Copy link

import requests APP_ID = "7bceec16" APP_KEY = "844902aaedb6f200acca45f3e3364288" GENDER = "Male" AGE = "20" WEIGHT_KG = "65" HEIGHT_CM = "175" exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise" exercise_text = input("Tell me which exercise did you do? ") headers = { "x-app-id": APP_ID, "x-app-key": APP_KEY, "Content-Type": "application/json" } parameters = { "query": exercise_text, "gender": GENDER, "weight_kg": WEIGHT_KG, "height_cm": HEIGHT_CM, "age": AGE } response = requests.get(url=exercise_endpoint, json=parameters, headers=headers) response.raise_for_status() tried this code a lot made many changes and yet getting this output **OUTPUT** raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: [https://trackapi.nutritionix.com/v2/natural/exercise](https://trackapi.nutritionix.com/v2/natural/exercise%60)

use GENDER = 'male' AGE = 20 int not str weight_kg = 65 same int not str height_cm = 175 then save response in new variable data or results data = response.json() print(data)

still the same no changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment