Error 400 - POST to upload file in bot framework emulator Locally Testing

Sangarsh S 0 Reputation points
2025-01-28T13:04:09.99+00:00

I am using the Bot Framework to create a chatbot. One of its features is the ability to upload PDF files. However, whenever a file is uploaded, it returns a 400 error, even if the file size is less than 2 MB. Currently using Python language for deployment and below is the code.

 async def on_message_activity(self, turn_context: TurnContext):
        """
        Handles user messages and checks for attachments.
        """
        print(f"Message activity received: {turn_context.activity}")
        if turn_context.activity.attachments and len(turn_context.activity.attachments) > 0:
            for attachment in turn_context.activity.attachments:
                print(f"Attachment detected: {attachment.name} of type {attachment.content_type}")
                await self._handle_incoming_attachment(attachment, turn_context)
        else:
            print("No attachment found in the message.")
            await turn_context.send_activity("Please attach a PDF file to upload.")
Azure AI Bot Service
Azure AI Bot Service
An Azure service that provides an integrated environment for bot development.
894 questions
{count} votes

1 answer

Sort by: Most helpful
  1. santoshkc 12,340 Reputation points Microsoft Vendor
    2025-01-28T14:38:49.2166667+00:00

    Hi @Sangarsh S,

    Thank you for reaching out to Microsoft Q&A forum!

    It seems like you're encountering a 400 error when uploading a file through the Bot Framework, even though the file size is under the limit. A possible cause for the error could be related to how the bot is handling the uploaded file in the on_message_activity method.

    Check to ensure that the bot is correctly processing the attachment and that the file upload URL is properly configured. Additionally, verify that the file type is supported (in your case, a PDF) and that the bot has the necessary permissions to handle file uploads. Sometimes, if the attachment is not correctly formatted or there is an issue with the connection to the server, the request might be rejected with a 400 error.

    You may also want to inspect the actual response message or logs from the Bot Framework to get more details about what caused the error. This could reveal issues like invalid request data, unsupported file formats, or problems in the code that processes the attachment. Debugging the request and checking the headers, body content, and the URL endpoint for uploading the file might help in pinpointing the issue.

    Lastly, double-check that you are using the correct bot endpoint in the Bot Framework Emulator, and ensure it's aligned with what your code expects.

    Please look into: Bot Framework and Create a bot with the Bot Framework SDK

    Thank you.

    0 comments No comments

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.