1

In the Django Framework I would like to post a file, received as an InMemoryUploadedFile, to a different server as soon as it is received.

I Have tried the solution in Django - post InMemoryUploadedFile to external REST api but the solution did not work for me.

Curl request given by External API:

curl --location --request POST 'https://app-sh.exmp.com/api/cret' \
--header 'Content-Type: multipart/form-data' \
--header 'api-key: Aa4f*******' \
--form 'kyc_doc=@/C:/Users/Shine/Downloads/image.png' \
--form 'company_name=name' \
--form 'company_country=IN' \
--form 'email=abc@xyz.com' 

How do I convert following into request.post() ?

From my current functional flow, the file is not sent to external api. Here is my code:

views.py

    def processUpload(request):
      data['company_country'] = request.POST.get('company_country')

      data['company_name'] = request.POST.get('company_name')
    
      #upload file
      data['kyc_doc'] = request.FILES['kyc_doc'].file.getvalue()

      files = {"kyc_doc":request.FILES['kyc_doc'].file.getvalue()}
      return service.doCurl(data,files)

service.py

   def doCurl(data,files):
      #.....
      # business logic here
      #
     del data['common_user_token']
     response_kcloud = curl.post('https://app-sh.exmp.com/api/cret',data,{ "api-key":acc.api_key},files)
    

curl.py

    def post(url,data,headers,ufile=None):

        curlheaders = {"Content-Type": "application/json"}

        #empty data
        if not data:
            raise Exception("data cannot be empty")
        #empty url
        if not url:
            raise Exception("Url cannot be empty")
        #base case for header
        if headers:
            for key in headers:
                curlheaders[key] = headers[key]
      
        if ufile is None:
            return requests.post(url, data=json.dumps(data), headers=curlheaders)
        else:
            return requests.post(url, data=data, headers=curlheaders,files=ufile)
4

0 回答 0