2

I have an API (created with FastAPI) that takes an uploaded Word document and returns the content. I deployed the API on heroku. Below you find the code of the API with a test_client. The test_client returns the wanted data.

API code:

import os
import uvicorn
from fastapi import FastAPI, UploadFile, File
import docx
from io import BytesIO

# Instantiate the app
app = FastAPI()

@app.post("/readdocument")
async def readdocument(file: UploadFile = File(...)):
    doc = docx.Document(BytesIO(await file.read()))
    fullText = []
    for para in doc.paragraphs:
        fullText.append(para.text)
    return '\n'.join(fullText)


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

Code test client:

import requests

filename = "path_to_word_document"
files = {'file': (filename, open(filename, 'rb'))}

response = requests.request("POST", 'https://readdocument.herokuapp.com/readdocument', files=files)

print(response.json())

As a next step I wanted to test providing the API via rapidapi. However I can't find a way to link the endpoint /readdocument with the file upload. I only found the request options json and data. Is there a possibility to include files?

Mazze
  • 383
  • 3
  • 13

0 Answers0