Issue with API Request: HTTP 422 Error for Missing Fields

Hi Anvil Support Team,

I’m encountering an issue with making a POST request to the Darwin V7 API using Anvil. The request consistently returns an HTTP 422 error indicating that fields workflow_id and filters are missing. However, the same request works perfectly when executed in Google Colab and via Curl. I suspect there might be an issue with the payload structure when using Anvil, but I can’t pinpoint the exact problem.

Here’s the context:

  • API Endpoint: https://darwin.v7labs.com/api/v2/teams/{team_slug}/items/assign
  • Payload Structure:
    {
    “filters”: {
    “dataset_ids”: [67890],
    “select_all”: true,
    “item_paths”: [“/folder_1”]
    },
    “assignee_id”: 45689,
    “workflow_id”: “bt56678-reed-5678-5f6c-6f7f9f0f0d5ds”
    }

Headers:

  • Authorization: ApiKey my_actual_api_key
  • Content-Type: application/json
  • Accept: application/json

Google Colab and Curl:

  • Google Colab: The request works without any issues.
  • Curl: The request works and returns a successful response.

Here is my Anvil server function:


import anvil.http
import anvil.server

@anvil.server.callable
def assign_items_to_user(user_id, dataset_id, folder_path, workflow_id):
    API_KEY = 'my_actual_api_key'
    url = "https://darwin.v7labs.com/api/v2/teams/{team_slug}/items/assign"

    payload = {
        "filters": {
            "dataset_ids": [dataset_id],
            "select_all": True,
            "item_paths": [folder_path]
        },
        "assignee_id": user_id,
        "workflow_id": workflow_id
    }
    
    headers = {
        "Authorization": f"ApiKey {API_KEY}",
        "Content-Type": "application/json",
        "Accept": "application/json"
    }

    print("Sending payload:", payload)
    print("Using headers:", headers)

    try:
        response = anvil.http.request(url, method="POST", json=payload, headers=headers)
        print(f"API Response: {response}")
        return response
    except anvil.http.HttpError as e:
        error_details = e.content if hasattr(e, 'content') else str(e)
        print(f"HTTP error details: {error_details}")
        return {"error": f"HTTP error 422: {error_details}"}
    except Exception as e:
        error_message = str(e)
        print(f"An error occurred: {error_message}")
        return {"error": error_message}

Error Details:

  • HTTP error 422: {'errors': {'code': 'INVALID_REQUEST', 'message': "Invalid request. Refer to 'detail' for specific errors.", 'status': 422, 'detail': {'missing_field': ['/workflow_id', '/filters']}}}

Steps I’ve Taken:

  1. Verified the payload structure and headers.
  2. Successfully ran the request in Google Colab and Curl.
  3. Ensured that the API key and all parameters are correctly set.

Is there something specific to how Anvil handles JSON payloads or headers that might be causing this issue? Any insights or suggestions to resolve this would be greatly appreciated.

Thank you!


Reference:

I think you want to pass your payload as the data argument rather than the json argument.

3 Likes

Thanks a lot owen.campbell. I tried your suggestion and it worked for me like a charm.

Truly much appreciated.

1 Like

Here is what I did for anyone interested:

import json
import anvil.http
import anvil.server

@anvil.server.callable
def assign_items_to_user(user_id, dataset_id, folder_path, workflow_id):
    API_KEY = 'your_actual_api_key'
    url = "https://darwin.v7labs.com/api/v2/teams/{team_slug}/items/assign"

    payload = {
        "filters": {
            "dataset_ids": [dataset_id],
            "select_all": True,
            "item_paths": [folder_path]
        },
        "assignee_id": user_id,
        "workflow_id": workflow_id
    }
    
    headers = {
        "Authorization": f"ApiKey {API_KEY}",
        "Content-Type": "application/json",
        "Accept": "application/json"
    }

    # Serialize payload to JSON string
    payload_json = json.dumps(payload)

    print("Sending payload:", payload_json)  # Debugging output
    print("Using headers:", headers)  # Debugging output

    try:
        response = anvil.http.request(url, method="POST", data=payload_json, headers=headers)
        response_content = response.get_bytes().decode('utf-8')  # Decode the response content
        print(f"API Response: {response_content}")
        return response_content
    except anvil.http.HttpError as e:
        error_details = e.content if hasattr(e, 'content') else str(e)
        print(f"HTTP error details: {error_details}")
        return {"error": f"HTTP error 422: {error_details}"}
    except Exception as e:
        error_message = str(e)
        print(f"An error occurred: {error_message}")
        return {"error": error_message}

You don’t need to use the json library. Anvil will do that for you if you set json=True in your request call.

2 Likes