SilentVoid13 / Templater

A template plugin for obsidian
https://silentvoid13.github.io/Templater
GNU Affero General Public License v3.0
3.26k stars 197 forks source link

Unable to create my own quote callout using tp.web.request - 400 error #1458

Closed DudeThatsErin closed 1 week ago

DudeThatsErin commented 1 month ago

Plugin information (please complete the following information):

Describe the bug I am trying to run the file at the bottom and it is returning a 400 error. I have tried everything I could think of and nothing is working. I have zero clue why this is happening.

I am using the docs here: https://api-ninjas.com/api/quotes

Created an account with a correct API key.

Expected behavior Retrieve a quote and put it in a callout.

Screenshots image

Additional context File:

module.exports = async (tp) => {
    // List of categories to choose from
    const categories = [
        "age", "alone", "amazing", "anger", "architecture", "art", "attitude", "beauty", "best",
        "birthday", "business", "car", "change", "communication", "computers", "cool", "courage",
        "dad", "dating", "death", "design", "dreams", "education", "environmental", "equality",
        "experience", "failure", "faith", "family", "famous", "fear", "fitness", "food", "forgiveness",
        "freedom", "friendship", "funny", "future", "god", "good", "government", "graduation", "great",
        "happiness", "health", "history", "home", "hope", "humor", "imagination", "inspirational",
        "intelligence", "jealousy", "knowledge", "leadership", "learning", "legal", "life", "love",
        "marriage", "medical", "men", "mom", "money", "morning", "movies", "success"
    ];

    // Use the suggester prompt to ask the user for a category
    const category = await tp.system.suggester(categories, categories, false);

    // Default category if none is provided
    const finalCategory = category ? category : "happiness";

    // Define the API key
    const api = 'REDACTED';

    try {
        // Make the API call
        const response = await tp.web.request(`https://api.api-ninjas.com/v1/quotes?category=${encodeURIComponent(finalCategory)}`, {
            method: 'GET',
            headers: { 'X-Api-Key': api }
        });

        // Check if the response is empty or invalid
        if (!response) {
            throw new Error("No response received from the API.");
        }

        // Check if the response status code is not 200
        if (response.status !== 200) {
            throw new Error(`API returned status code: ${response.status}`);
        }

        // Parse the response
        const quoteData = JSON.parse(response);

        // Check if the response contains quotes
        if (quoteData && quoteData.length > 0) {
            const quote = quoteData[0];
            const author = quote.author || "Unknown"; // Handle missing author
            const quoteText = quote.quote;

            // Return the quote in the callout format
            return `> [!author]+ ${author}\n> ${quoteText}`;
        } else {
            return "No quote found for this category.";
        }
    } catch (error) {
        // Log detailed error message to help with debugging
        console.error("Error fetching the quote:", error);
        return `Error fetching the quote: ${error.message}`;
    }
};
Zachatoo commented 1 month ago

Can you check the console to see what the response is from the API? Is it a CORS error?

Zachatoo commented 1 week ago

Stale, closing