'Provide API on rapidapi with file upload
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?
Solution 1:[1]
const arr = [{ id: '00001', flag: true }, { id: '00001', flag: true }, { id: '', flag: true, },];
/**
* @param {{ id: string, flag: boolean }[]} arr The array to check
*/
function meetsCriteria(arr) {
let id = "";
return !arr.some( o => {
if (!o.flag) return true;
if (id === "") {
id = o.id;
} else if (id !== o.id && o.id !== "") {
return true;
}
return false;
});
}
console.log( meetsCriteria(arr) );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SoulKa |
