Is there a way to use a user input variable into a specific parameter in an fetch API function?

39 views Asked by At

I want to use the const value as a variable that can be slotted into the API URL, specifically in the ingr= section. I don't know how to make a user input interface in this case, and also I'm not sure how to use the body within the fetch function to specify where in the URL that I want to slot in the user input variable. Please advise.

const value = "1 apple"

fetch('https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data?ingr=&nutrition-type=cooking', {
    "method": 'GET',
    "body": JSON.stringify({ search: value }),
    "headers": {
        'X-RapidAPI-Key': 'c4fd150a50msh880d519af0bfe67p156134jsn6aa2bf154889',
        'X-RapidAPI-Host': 'edamam-edamam-nutrition-analysis.p.rapidapi.com'
    }
})

.then(response => response.json())
.then(response => {

    console.log(response);
})
 .catch(error => {
    console.error(error);
});

I want the expected API to look something like this (the key and id aren't important)

https://api.edamam.com/api/nutrition-data?app_id=d785be51&app_key=63b177d15cdfdcfab6b686540fd8e8cd&ingr=1 banana&type=public

0

There are 0 answers