- I am posting a request in the form, which takes a pdf as an input defined in OmniDocsReceiver.py. running on host127.0.0.1, port8000.
- Callback url CallbackUrl = "http://0.0.0.0:8030/OmniDocsSender/"
async def omnidocs_receiver(uploaded_file: UploadFile = File(...)):
    try:
        # pass the document
        if not uploaded_file:
            logger.error("File not uploaded", exc_info=True)
            return {"message": "No upload file sent"}
        else:
            logger.info("Received file", exc_info=True)
            # specify the location
            file_location = f"{gstr_uploaded_file}/{uploaded_file.filename}"
            # copy the file at the specific location
            with open(file_location, "wb+") as file_object:
                logger.info("File copied to local input folder", exc_info=True)
                shutil.copyfileobj(uploaded_file.file, file_object)
            # read the file and encode it
            with open(file_location, "rb") as pdf_file:
                logger.info("File encoded to base64", exc_info=True)
                encoded_string = base64.b64encode(pdf_file.read())
            # post the file
            payload = {
                "InputDocument": str(encoded_string.decode('utf-8')),
                "ContextKey": ''.join(random.choices(string.ascii_uppercase + string.digits, k=5)) + "_" + str(
                    uploaded_file.filename),
                "CallbackUrl": CallbackUrl,
                "UseCaseCode": use_case,
                "FileFormat": "pdf",
            }
            response = requests.post(url=processs_document_url, data=json.dumps(payload))
            logger.info("Document sent to document for masking", exc_info=True)
            print("Response text while sending=", response.text.encode('utf8'))
        return response.text.encode('utf8')
    except Exception as e:
        logger.error(str(e), exc_info=True)
- I am putting a postman post request on - http://127.0.0.1:8000/OmniDocsReceiver/and passing a pdf in- uploaded_fileparam.
- Which in turn sends a response in the below form when the execution is completed at the mentioned - callbackurl
Json Response on successful completion
{
"TransactionRefNo": “<t_id>”,
"Results": {
"ContextKey": "<context_key>",
"MaskedDocument": "base_64 string",
"QualitativeAnalysis": "PossibleMissingNumber"
},
"ResponseCode": "2001",
"HttpResponseCode": 200,
"ProcessingStatus": 2
}
- I have the callback running on host 0.0.0.0port8030, but while testing it through postman the data is not recevied.
from fastapi import FastAPI, File, UploadFile, Request
from configuration import *
import base64
import shutil
import string
import random
import uvicorn
import datetime
app = FastAPI()
# Folder path
gstr_save_masked_file_path = CAL_SAVE_MASKED_DOCUMENT_PATH
print(gstr_save_masked_file_path)
@app.post("/OmniDocsSender/")
async def omnidocs_sender():
    try:
        logger.info("Document received in callback", exc_info=True)
        # print(request)
        # await request.json()
        current_time = datetime.datetime.now()
        recevied_file = "something" + str(current_time)
        res = ''.join(random.choices(string.ascii_uppercase +
                                     string.digits, k=4))
        # save the file by copying the file at the specific location
        file_location = f"{gstr_save_masked_file_path}/{res}"
        print("location and unique key generated")
        with open(file_location, "w") as file_object:
            logger.info("File successfully saved in the local storage after masking", exc_info=True)
            print("writing file")
            file_object.write(recevied_file)
        return "Done"
    except Exception as e:
        print(str(e))
if __name__ == "__main__":
    uvicorn.run(app)
- On the callback, the steps I have defined is not getting executed but the file is getting stored on the server side.
 
    