I built a FastAPI service that processes uploaded PDF files and extracts their text content. The API works perfectly when deployed on Heroku and I can test it successfully with my own client code.
My working API code:
import os
import uvicorn
from fastapi import FastAPI, UploadFile, File
from PyPDF2 import PdfReader
from io import BytesIO
app = FastAPI()
@app.post("/extract-text")
async def extract_text(uploaded_file: UploadFile = File(...)):
pdf_reader = PdfReader(BytesIO(await uploaded_file.read()))
extracted_content = []
for page in pdf_reader.pages:
extracted_content.append(page.extract_text())
return '\n'.join(extracted_content)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))
Test client that works:
import requests
file_path = "sample_document.pdf"
file_data = {'uploaded_file': (file_path, open(file_path, 'rb'))}
api_response = requests.post('https://myapp.herokuapp.com/extract-text', files=file_data)
print(api_response.json())
Now I want to publish this API through RapidAPI marketplace but I’m stuck. When I try to configure the endpoint on RapidAPI, I only see options for JSON and form data parameters. There doesn’t seem to be any way to specify file upload functionality for the /extract-text endpoint. Can anyone help me figure out how to properly configure file uploads on RapidAPI?
Had this exact problem last year with my image processing API. RapidAPI’s file handling is pretty limited. Here’s what worked: create a wrapper endpoint that takes the file as multipart form data, then process it like your current code does. Set it up as “form-data” in RapidAPI’s console - not JSON. Make sure the parameter type is “file” in their config panel. Just heads up - I had issues with larger files, so test different PDF sizes before you launch. Check their dev portal docs too - there’s a section on file parameters that should help.
rapidapi doesn’t directly support file uploads, unfortunately. you might want to modify your endpoint to take a base64 string instead of UploadFile. then decode it back to bytes in your handler. it’s a hassle, but many users do this workaround.
RapidAPI does support file uploads, but their interface is confusing as hell. When you’re setting up your endpoint, create the parameter as “Body” type - don’t use “Query” or “Header”. Pick “form-data” for content type and set the parameter to “file”. Your FastAPI code should work fine once you’ve got this right. I’ve published a bunch of file processing APIs there and it’s all about nailing the parameter setup in their console. Their docs suck for this, but check the “Advanced” settings when creating parameters - that’s where you’ll find the file option. Test everything with their built-in tool before going live since file size limits can be all over the place.