Help

422 Client Error: Unprocessable Entity

Topic Labels: ImportingExporting
Solved
Jump to Solution
11287 2
cancel
Showing results for 
Search instead for 
Did you mean: 
vidhun_prakash
4 - Data Explorer
4 - Data Explorer

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.

1 Solution

Accepted Solutions
James_Timmins
5 - Automation Enthusiast
5 - Automation Enthusiast

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?

See Solution in Thread

2 Replies 2
James_Timmins
5 - Automation Enthusiast
5 - Automation Enthusiast

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?

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.