When I am sending a base64 string encoded file to azure functions for http trigger, it is sending a 413 error.

Ayush T24Global.com 0 Reputation points
2024-07-20T13:22:11.9266667+00:00

When I am sending a base64 string encoded file to azure functions for http trigger, it is sending a 413 error. {"statusCode":413,"message":"request entity too large"}. The base64Source: size.txt

My code:

import azure.functions as func
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence.models import AnalyzeResult
import logging, base64, imghdr

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
    
@app.route(route="http_dataretrieval")
def http_dataretrieval(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    endpoint = "" #blocked
    credential = AzureKeyCredential("")#blocked

    base64source = bytes(eval(req.get_body().decode()).get("base64Source"), encoding='utf-8')
    yo = base64.b64decode(base64source)

    extension = "." + imghdr.what(None, h=yo)

    with open("file" + extension, "wb") as f:
        f.write(yo)

    with open("file" + extension, "rb") as f:
        document_intelligence_client = DocumentIntelligenceClient(endpoint, credential)
        
        poller = document_intelligence_client.begin_analyze_document(
            "prebuilt-invoice", analyze_request=f, content_type="application/octet-stream"
        )

        result: AnalyzeResult = poller.result()

        invoice = result.documents[0]

        processed_dict = dict()

        for key, value in invoice.fields.items():
            if key == "Items":
                items = invoice.fields.get("Items")
                processed_dict["Items"] = dict()
                for id, item in enumerate(items.get("valueArray")):
                        processed_dict["Items"][f"{id + 1}"] = dict()
                        item_description = item.get("valueObject").get("Description")
                        if item_description:
                            processed_dict["Items"][f"{id + 1}"]["Description"] = item_description.get("content")
                        item_quantity = item.get("valueObject").get("Quantity")
                        if item_quantity:
                            processed_dict["Items"][f"{id + 1}"]["Quantity"] = item_quantity.get("content")
                        unit = item.get("valueObject").get("Unit")
                        if unit:
                            processed_dict["Items"][f"{id + 1}"]["Unit"] = unit.get("content")
                        unit_price = item.get("valueObject").get("UnitPrice")
                        if unit_price:
                            unit_price_code = (
                                unit_price.get("valueCurrency").get("currencyCode")
                                if unit_price.get("valueCurrency").get("currencyCode") else ""
                            )
                            processed_dict["Items"][f"{id + 1}"]["UnitPrice"] = {'Value': unit_price.get("contnet"), 'CurrencyCode': unit_price_code}
                            
                        product_code = item.get("valueObject").get("ProductCode")
                        if product_code:
                            processed_dict["Items"][f"{id + 1}"]["ProductCode"] = product_code.get("content")
                        item_date = item.get("valueObject").get("Date")
                        if item_date:
                            processed_dict["Items"][f"{id + 1}"]["ItemDate"] = item_date.get("content")
                        tax = item.get("valueObject").get("Tax")
                        if tax:
                            processed_dict["Items"][f"{id + 1}"]["Tax"] = tax.get("content")
                        amount = item.get("valueObject").get("Amount")
                        if amount:
                            processed_dict["Items"][f"{id + 1}"]["Amount"] = amount.get("content")
                continue
            processed_dict[key] = value.get("content", "lol")

        

        return func.HttpResponse(f"{processed_dict}", status_code=200)
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,856 questions
Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
1,612 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.