numbers-to-words

פרמיום
על ידי Saddem-Khadra | מְעוּדכָּן 7 days ago | Tools
פּוֹפּוּלָרִיוּת

9.2 / 10

חֶבִיוֹן

61ms

רמת שירות

100%

Health Check

100%

חזרה לכל הדיונים

[HELP] 422 Unprocessable Content

Rapid account: Nemetrix 05
nemetrix05
a month ago

Hi there

I tested the API with the structure provide by the docs, but i am getting the following error:

422 Unprocessable Content

{
“detail”: [
{
“type”: “json_invalid”,
“loc”: [
“body”,
1
],
“msg”: “JSON decode error”,
“input”: {},
“ctx”: {
“error”: “Expecting value”
}
}
]
}

Could you help me to check please

Rapid account: Saddem Khadra
Saddem-Khadra Commented a month ago

The issue with the JSON was that the payload object was not being properly converted to a JSON string before sending it in the request body. In the original code, the body field of the options object was directly assigned the payload object:

body: { number, language: "en" }

However, when sending data in a fetch request, the body needs to be a JSON string. So, the payload object needed to be converted into a JSON string using JSON.stringify():

body: JSON.stringify({
    number: number,
    delete_from_sentence: null,
    currency: "دينار",
    decimal_currency: "مليم",
    separator: "و",
    decimal: 3,
    language: "ar"
})

By making this change, the payload was correctly formatted as a JSON string before being sent in the request body, resolving the issue.

Rapid account: Nemetrix 05
nemetrix05 Commented a month ago

Working fine !!! Thanks so much for your support, The API works really nice for I need.
for curiosity what it was the issue with the JSON?

Rapid account: Saddem Khadra
Saddem-Khadra Commented a month ago

async function numberToEnglish(number) {
const url = ‘https://numbers-to-words1.p.rapidapi.com/api/converter/’;

const options = {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        'X-RapidAPI-Key': '7d28eec811msh4b872f89777f037p151b9djsnae4e9d77a101',
        'X-RapidAPI-Host': 'numbers-to-words1.p.rapidapi.com'
    },
    body: JSON.stringify({
        number: number,
        delete_from_sentence: null,
        currency: "دينار",
        decimal_currency: "مليم",
        separator: "و",
        decimal: 3,
        language: "ar"
    })
};

if (typeof number === 'number') {
    try {
        let response = await fetch(url, options);
        if (response.ok) {
            let data = await response.json();
            return data.message;
        }
    } catch (error) {
        console.error(error);
        return null; // Return null or handle the error as needed
    }
} else {
    console.log('Please enter a number');
    return null; // Return null or handle the error as needed
}

}

(async () => {
console.log(await numberToEnglish(45));
})();

Rapid account: Nemetrix 05
nemetrix05 Commented a month ago

Hello Saddem,

Thanks for the quick answer, I checked the points you send me and I am still getting the error, maybe it might be an error I can’t see.
Here is the code I am running:

async function numberToEnglish(number) {
const url = ‘https://numbers-to-words1.p.rapidapi.com/api/converter/’;

const options = {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        'X-RapidAPI-Key': '7d28eec811msh4b872f89777f037p151b9djsnae4e9d77a101',
        'X-RapidAPI-Host': 'numbers-to-words1.p.rapidapi.com'
    },
    body: { number, language: "en" }
};

if (typeof number === 'number') {
        try {
            let response = await fetch(url, options);
            if(response.ok) {
                return response.json().message;
            }
        }
        catch(error) {
            return console.log(error);
        }
}
console.log('Please enter a number');

}

console.log(numberToEnglish(45));

Rapid account: Saddem Khadra
Saddem-Khadra Commented a month ago

Hi,

Thanks for reaching out. It looks like the API is having trouble decoding the JSON input you’re sending. The error message “Expecting value” typically indicates that there’s a syntax issue in the JSON structure.

Here are a few things to check:

  1. Proper JSON Structure:

    • Ensure all key-value pairs are separated by colons : and are inside curly braces {}.
    • Check if you have any extra commas, unclosed braces, or mismatched quotation marks.
  2. Data Types:

    • Make sure that string values are enclosed in double quotes "".
    • Confirm that you aren’t accidentally mixing string and numeric types.

If possible, can you share the JSON input you’re sending? It will help me pinpoint the issue more accurately.

I hope these suggestions help. If you have further details or questions, let me know, and I’d be happy to assist.

הצטרף לדיון - הוסף תגובה למטה:

התחבר / הירשם כדי לפרסם תגובות חדשות