422 Client Error: Unprocessable Entity

Hi!

I am trying to write data to a airtable from Rasa chatbot. But when i run the code, it returns me a 422 Client Error: Unprocessable Entity for url: https://api.airtable.com/v0/<base ID/<table_name> error.

load_dotenv()

airtable_api_key = os.getenv("AIRTABLE_API_KEY")
base_id = os.getenv("BASE_ID")
table_name = os.getenv("TABLE_NAME")

def create_convo_record(category, price, storage, ram, battery):
    request_url = f"https://api.airtable.com/v0/{base_id}/{table_name}"

    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json",
        "Authorization": f"Bearer {airtable_api_key}",
    }

    data = {
        "fields": {
            "category": category,
            "price": price,
            "storage": storage,
            "ram": ram,
            "battery": battery,
        }
    }

    try:
        response = requests.post(
            request_url, headers=headers, data=json.dumps(data)
        )
        response.raise_for_status()
    except requests.exceptions.HTTPError as err:
        raise SystemExit(err)

    print(f"Response status code: {response.status_code}")
    return response

Here is my code for the same and the data to the table if fetched using this piece of code

class SubmitConvoForm(Action):

    def name(self) -> Text:
        return "submit_convo_form"

    async def run(
            self, dispatcher, tracker: Tracker, domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        category = tracker.get_slot("category")
        price = tracker.get_slot("price")
        storage = tracker.get_slot("storage")
        ram = tracker.get_slot("ram")
        battery = tracker.get_slot("battery")

        response = create_convo_record(category, price, storage, ram, battery)

        dispatcher.utter_message("Thanks, your answers have been recorded!")

        return []

I am stuck with this forever and it will be a great help if someone points me in the right direction. Thanks.

Hi Vidhun! Can you share the output of json.dumps(data)? It’s possible that something is getting encoded in a format that doesn’t upload as you expect.

Also, can you verify that the field names are category, price, storage, ram, and battery, spelled with lowercase?

1 Like

Hi James!

Thank you for your reply. I have solved the issue. The problem was that some of my values were digits. So i needed to set the field type to number in the airtable - trivial and silly mistake which i overlooked.

For those who are facing the Error 422: unprocessable entity, i would recommend to add print(response.json()) with your code to check what’s going wrong.

1 Like

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.