Error uploading file via REST API

Options

Hi together,

we are struggeling uploading files to OTCS via REST API. When we user the Postman integrated file selection to select the file we want to send, everything works fine. But when i'm trying to do the same with python it does not work. I'm getting the error "File could not be found within the upload directory".

Anyone has solved that issue?

Here my example code:

Kind regards

Patrick

Tagged:

Comments

  • Your form posting I think is wrong because CS expects the request to be application form encoding but the problem is you are sending everything you need as the HTTP body that is not how OT wants it

    if you see this link there is some starter code you can see painstakingly making the POSTS correctly


    if you look at my word document you can see how the file is being sent nowhere in the code will CS have things like c:\afile.the file should be a file object .That is what I think is happening. If you use postman it should show you http code or even python code.

    I usually construct a json string for the body and one for the file.

    see IMPLEMENTATION NOTES in developer.opentext.com

    in something recently I had to do i had to use fiddler in my java code to figure out what I was doing wrong :)

    Just my $0.02

  • Aseveli
    edited June 6, 2022 #3
    Options

    Hi Patrick,

    I was able to get this working by using the MultipartEncoder from the Requests Toolbelt python library.

    Here is a snippet of some code I used.


    parentFolderId = 9999999
    putNodeUrl = "http://contentserver/otcs/llisapi.dll/api/v2/nodes"
    
    csFileName = "tester.csv"
    putFileName = "c:\\working\\uploadtest.csv"
    
    mp_encoder = MultipartEncoder(
     fields={
      'type': '144',
      'parent_id': str(parentFolderId),
      'name': csFileName,
      'file': (csFileName, open(putFileName, 'rb'))
     }
    )
    
    putHeaders = {'OTCSTicket': otcsticket, 'Content-Type': mp_encoder.content_type}
    
    myResponse = requests.post(putNodeUrl, data=mp_encoder, headers=putHeaders)
    
    print (myResponse.status_code)
    
    # For successful API call, response code will be 200 (OK)
    if(myResponse.ok):       
     print("File was saved to Content Server.")
    else:
     # If response code is not ok (200), print the resulting http error code with description
     myResponse.raise_for_status()
    


    Hopefully this helps.

    Todd.