导入
食谱营养信息组件
学习如何获取交互式 Spoonacular 小部件,这些小部件可显示食谱的营养数据。
...
菜谱 ID
| 菜谱名称 | ID |
|---|---|
| 经典水果沙拉 | 639600 |
| 德克萨斯风味汉堡 | 663050 |
| 烤西冷牛排 | 633790 |
| 素食查纳马萨拉咖喱 | 1096306 |
| 比利时热巧克力 | 634818 |
1GET https://api.spoonacular.com/recipes/{id}/nutritionLabel
请求参数
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| id | 数字 | 必填 | 获取营养标签小部件的菜谱标识符。 |
| defaultCss | 布尔值 | 可选 | 若设为 true,则在响应中添加默认 CSS。该参数默认值为 true。 |
| showOptionalNutrients | 布尔值 | 可选 | 若设为 true,则在响应中显示可选营养成分。该参数默认值为 false。 |
| showZeroValues | 布尔值 | 可选 | 若设为 true,则在响应中显示零值。该参数默认值为 false。 |
| showIngredients | 布尔值 | 可选 | 若设为 true,则在响应中显示食材列表。该参数默认值为 false。 |
1// 导入所需库 2import fetch from 'node-fetch'; 3 4// 在此处定义任意菜谱的 ID 5const id = '{{RECIPE_ID}}'; 6 7// 定义接口地址 8const endpointUrl = new URL(`https://api.spoonacular.com/recipes/${id}/nutritionLabel`); 9 10// 定义 API 密钥 11const apiKey = '{{API_KEY}}'; 12 13// 定义请求头参数 14const headerParameters = { 15 'x-api-key': apiKey, 16 'content-type': 'text/html', 17}; 18 19// 定义查询参数 20const queryParameters = new URLSearchParams({ 21 defaultCss: true, 22}); 23 24// 设置 API 请求选项 25const endpointOptions = { 26 method: 'GET', 27 headers: headerParameters, 28}; 29 30// 发起 API 请求的函数 31async function recipeNutritionLabelWidget(url, options) { 32 try { 33 // 使用 fetch 方法调用 Spoonacular 接口 34 const response = await fetch(url, options); 35 36 // 自定义函数:打印 API 响应结果 37 printResponse(response); 38 } catch (error) { 39 // 自定义函数:打印错误信息 40 printError(error); 41 } 42} 43 44// 将查询参数附加到接口地址 45endpointUrl.search = queryParameters; 46 47// 调用函数发起 API 请求 48recipeNutritionLabelWidget(endpointUrl, endpointOptions);
1POST https://api.spoonacular.com/recipes/visualizeNutrition
请求参数
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| ingredientList | 字符串 | 必填 | 菜谱的食材列表,每行一个食材及其用量。示例:chocolate2 cups of coffee1.4 liters almond milk需将此参数作为请求体传递,而非查询参数或路径参数。 |
| servings | 数字 | 可选 | 菜谱的份量数量。需作为请求体传递,而非查询参数或路径参数。 |
| defaultCss | 布尔值 | 可选 | 若设为 true,则在响应中添加默认 CSS。该参数默认值为 true。需作为请求体传递。 |
| showBacklink | 布尔值 | 可选 | 若设为 true,则在响应中显示指向 Spoonacular 的返回链接,且无配额成本。该参数默认值为 true。需作为请求体传递。 |
| language | 字符串 | 可选 | 响应的语言。可为 en 或 de。默认值为 en。需作为查询参数或路径参数传递。 |
1// 定义接口地址 2const endpointUrl = new URL(`https://api.spoonacular.com/recipes/visualizeNutrition`); 3 4// 定义请求头参数 5const headerParameters = { 6 'x-api-key': apiKey, 7 'accept': 'text/html', 8 'content-type': 'application/x-www-form-urlencoded', 9}; 10 11// 定义查询参数 12const queryParameters = new URLSearchParams({ 13 language: 'en', 14}); 15 16// 定义请求体参数 17const bodyParameters = { 18 ingredientList: '1 apple\n \ 19 1 cup of olive oil\n \ 20 1.4 liters milk\n \ 21 2 1/2 salmon fillets\n \ 22 kale', 23 servings: 3, 24 defaultCss: true, 25}; 26 27// 将请求体参数转换为表单数据 28const bodyFormData = new URLSearchParams(); 29for (key in bodyParameters) { 30 bodyFormData.append(key, bodyParameters[key]); 31} 32 33// 设置 API 请求选项 34const endpointOptions = { 35 method: 'POST', 36 headers: headerParameters, 37 body: bodyFormData, 38}; 39 40// 将查询参数附加到接口地址 41endpointUrl.search = queryParameters; 42 43// 调用函数发起 API 请求 44recipeNutritionWidget(endpointUrl, endpointOptions);
1GET https://api.spoonacular.com/recipes/{id}/nutritionWidget
请求参数
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| id | 数字 | 必填 | 获取营养小部件的菜谱标识符。 |
| defaultCss | 布尔值 | 可选 | 若设为 true,则在响应中添加默认 CSS。该参数默认值为 true。 |
1// 定义任意菜谱的 ID 2const id = '{{RECIPE_ID}}'; 3 4// 定义接口地址 5const endpointUrl = new URL(`https://api.spoonacular.com/recipes/${id}/nutritionWidget`); 6 7// 定义请求头参数 8const headerParameters = { 9 'x-api-key': apiKey, 10 'accept': 'text/html', 11}; 12 13// 定义查询参数 14const queryParameters = new URLSearchParams({ 15 defaultCss: true, 16}); 17 18// 设置 API 请求选项 19const endpointOptions = { 20 method: 'GET', 21 headers: headerParameters, 22}; 23 24// 将查询参数附加到接口地址 25endpointUrl.search = queryParameters; 26 27// 调用函数发起 API 请求 28recipeNutritionWidget(endpointUrl, endpointOptions);