Search code examples
pythonmultipartform-data

Python : Passing form-data directly in fast-api with UI


  • I am calling a API of OmniDocs, I am able to Add Document using Postman, passing it as form-data.
  • When I am trying to do the same in python it is returning Bad request.
  • NGOAddDocumentBDO Value
<NGOAddDocumentBDO>
    <cabinetName>samplecabinet</cabinetName>
    <folderIndex>3185</folderIndex>
    <documentName>Restweb postman</documentName>
    <userDBId></userDBId>
    <volumeId>1</volumeId>
    <accessType>S</accessType>
    <createdByAppName>txt</createdByAppName>
    <enableLog>Y</enableLog>
    <versionFlag>N</versionFlag>
    <textAlsoFlag></textAlsoFlag>
    <ownerType>U</ownerType>
    <ownerIndex>2</ownerIndex>
    <nameLength></nameLength>
    <thumbNailFlag>N</thumbNailFlag>
    <imageData></imageData>
    <encrFlag>N</encrFlag>
    <passAlgoType>MD5</passAlgoType>
    <userName>test123</userName>
    <userPassword>Test@1234</userPassword>
    <comment></comment>
    <locale>en_US</locale>
    <NGOAddDocDataDefCriterionBDO>
        <dataDefIndex>22</dataDefIndex>
        <dataDefName>DIGI2</dataDefName>
        <NGOAddDocDataDefCriteriaDataBDO>
            <indexId>43</indexId>
            <indexType>I</indexType>
            <indexValue>123</indexValue>
        </NGOAddDocDataDefCriteriaDataBDO>
    </NGOAddDocDataDefCriterionBDO>
    <NGOAddDocKeywordsCriterionBDO>
        <keyword></keyword>
    </NGOAddDocKeywordsCriterionBDO>
</NGOAddDocumentBDO>

enter image description here

  • Postman output

enter image description here

  • Using fastapi in python, I am passing 'content-type': 'multipart/form-data', but not able to determine where the request is going wrong, if it is working fine with postman, not able to figure out the problem in fast-api.
    lstr_add_document_response = "Empty"
            lxml_add_document = """
            <NGOAddDocumentBDO>
            <cabinetName>samplecabinet</cabinetName>
            <folderIndex>3185</folderIndex>
            <documentName>Restweb postman</documentName>
            <userDBId></userDBId>
            <volumeId>1</volumeId>
            <accessType>S</accessType>
            <createdByAppName>txt</createdByAppName>
            <enableLog>Y</enableLog>
            <versionFlag>N</versionFlag>
            <textAlsoFlag></textAlsoFlag>
            <ownerType>U</ownerType>
            <ownerIndex>2</ownerIndex>
            <nameLength></nameLength>
            <thumbNailFlag>N</thumbNailFlag>
            <imageData></imageData>
            <encrFlag>N</encrFlag>
            <passAlgoType>MD5</passAlgoType>
            <userName>test123</userName>
            <userPassword>Test@1234</userPassword>
            <comment></comment>
            <locale>en_US</locale>
            <NGOAddDocDataDefCriterionBDO>
            <dataDefIndex>22</dataDefIndex>
            <dataDefName>DIGI2</dataDefName>
            <NGOAddDocDataDefCriteriaDataBDO>
            <indexId>43</indexId>
            <indexType>I</indexType>
            <indexValue>123</indexValue>
            </NGOAddDocDataDefCriteriaDataBDO>
            </NGOAddDocDataDefCriterionBDO>
            <NGOAddDocKeywordsCriterionBDO>
            <keyword></keyword>
            </NGOAddDocKeywordsCriterionBDO>
            </NGOAddDocumentBDO>
            """
            lstr_headers = {'content-type': 'multipart/form-data'}
            lstr_data = {'NGOAddDocumentBDO': lxml_add_document, 'file': open('/home/donny/Desktop/omnidocs/output-document/8VQUI_something.pdf', "wb+")}
            try:
                lstr_add_document_response = requests.post(gstr_omnidocs_add_document_service,
                                                           data=lstr_data,
                                                           headers=lstr_headers)
                print(gstr_omnidocs_add_document_service)
                print(lstr_data)
                print(lstr_headers)
            except Exception as e:
                logger.error(str(e), exc_info=True)
            print("Response text=", lstr_add_document_response.text.encode('utf8'))
            print(lstr_add_document_response.content)
    
            # replace &lt; with <
            new_lstr_add_document_response = (lstr_add_document_response.text).replace("&lt;", "<")
    
            # get the response from the add document response
            lstr_soup_response = BeautifulSoup(new_lstr_add_document_response, features="xml")
            return lstr_soup_response
        except Exception as e:
            logger.error(str(e), exc_info=True)


Solution

  • Postman on right side should have icon </> to open function Code snippet which can generate code for different languages - even for Python (requests and http.client)

    enter image description here

    It gives me code which sends file in files=...

    import requests
    
    url = "http://10.10.2.41:8003/OmniBook"
    
    payload={'NGOAddDocumentBDO': '<XML>'}
    files=[
      ('file',('file',open('/path/to/file','rb'),'application/octet-stream'))
    ]
    headers = {}
    
    response = requests.request("POST", url, headers=headers, data=payload, files=files)
    
    print(response.text)
    

    But I couldn't test it.