I have developed an API using FastAPI that accepts a Word document upload and extracts its contents. This API is currently hosted on Heroku and functions properly with the following implementation. Below is the code for the API, which successfully retrieves the desired information.
API Implementation:
import os
import uvicorn
from fastapi import FastAPI, UploadFile, File
import docx
from io import BytesIO
app = FastAPI()
@app.post("/extractdoc")
async def extractdoc(file: UploadFile = File(...)):
document = docx.Document(BytesIO(await file.read()))
text_content = []
for paragraph in document.paragraphs:
text_content.append(paragraph.text)
return '\n'.join(text_content)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
Test Client Example:
import requests
file_path = "path_to_your_word_document"
files = {'file': (file_path, open(file_path, 'rb'))}
response = requests.post('https://extractdoc.herokuapp.com/extractdoc', files=files)
print(response.json())
Now, I’m interested in integrating this API with RapidAPI, but I’m having trouble figuring out how to connect the /extractdoc
endpoint for file uploads. The options I came across just mention JSON and data requests. Is it possible to handle file uploads?