Facing issue while trying to download file using Rest API

Hi All,

i was trying to list and download file from a folder.

For listing,

http://CSurl/otcs/cs.exe/api/v2/nodes/<id>/nodes where am parsing the json and taking the data id. Then i am giving as input the above received document id to below api

http://CSurl/otcs/cs.exe/api/v2/nodes/<id>/content

When am trying to execute the API from postman, its is displaying content like showing the content inside that word document, instead of downloading.

Can some one please help me with an input here so that i can download the doc through an API?

Thanks in advance.

Answers

  • Hello,

    This might be a postman question.. but if you want that utility to download the file, instead of using Send, try 'Send and Download'. There is a drop down on the Send button to choose the other option.

    Hope this helps.

  • Hello @HSS2021 ,

    Now the response is getting downloaded with an error required input argument func not specified.

    Am not getting the correct file downloaded here.

  • Hi,

    Send and download worked here. while writing the code in python, how i can do the same and download content?

    Writing response into a file will work?

  • Hello,

    And yes, writing the response to a file will work in Python. You can use the requests library to do that and iterate through the request to write it into a file chunk by chunk

    with requests.get(URL,headers=headers,stream=True) as r:
    r.raise_for_status()
    with open(local_filename, 'wb') as f:
    for chunk in r.iter_content(chunk_size=8192):
    f.write(chunk)

  • Hi @HSS2021 ,

    Thanks for your time to looking in to this, unfortunately using above snippet in python, am able to generate file, but its coming with no content.

    When further checked its not entering the above for loop.

    Do you have any clue why it is?

  • HSS2021
    HSS2021 E Member
    edited September 18 #7

    I am not sure what is wrong with your code. however this works for me. Its a rudimentary script (obviously with its faults and issues so take it as is) but it works for quick scripting

    import requests

    URL = "http://SERVERNAME/OTCS/cs.exe/api/v2/nodes/187261/content?action=download"
    ticket = "TicketFromApi/V1/Auth"

    headers ={'otcsticket':ticket}
    local_filename = "C:\\edit.docx"
    with requests.get(URL,headers=headers,stream=True) as r:
    r.raise_for_status()
    with open(local_filename, 'wb') as f:
    for chunk in r.iter_content(chunk_size=8192):
    f.write(chunk)

    In the above example, nodeID is 187261, and you are posting the file to C:\edit.docx
    insert the otcsticket under the 'ticket' variable.