SharePoint REST API 2.0 Redirecting to the exact same endpoint url - Get File Content

Victor Melo 0 Reputation points
2025-01-23T23:43:46.85+00:00

I am clueless about this!
Was trying to Get File Content by using SharePoint REST API 2.0 - Send An HTTP Request to SharePoint action using method GET, endpoint:
/_api/v2.0/sites/{site-id}/drives/{drive-id}/items/{item-id}/content

Result is failing the online flow with a Redirect error with "source" with exact same of the endpoint queried. response bellow:

{
    "status": 302,
    "message": "",
    "source": "https://{tanant}.sharepoint.com/sites/{site}/_api/v2.0/sites/{site-id}/drives/{drive-id}/items/{item-id}/content",
    "errors": []
}

Iknow the file is there because i can use teh exact same ids in other endpoints and it finds the file there. I am totally clueless about this...

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,407 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Victor Melo 0 Reputation points
    2025-03-11T19:51:00.14+00:00

    I finally figure out a way to extract file content only using REST 2.0 endpoints! The solution is bellow:

    DISCLAIMER: I still don't know why using the /content return me the Redirect error with a redirecting url to the same url requested (the same as the original {item-path} queried), if you know why and how can i make the /content endpoint work direcly, let me know! I also don't know if this is the best solution, but it is still one solution, and remember, you can also use 1.0 endpoints to avoid this problem compleatly.

    1- Run any of the endpoints I used but without the /content

    GET /_api/v2.0/sites/{siteId}/drives/{drive-id}/items/{item-id}
    GET /_api/v2.0/sites/{siteId}/drives/{drive-id}/root:/{item-path}
    GET /_api/v2.0/sites/{siteId}/drives/{drive-id}/items/root:/{item-path}
    GET /_api/v2.0/drives/{drive-id}/items/{item-id}
    GET /_api/v2.0/drives/{drive-id}/root:/{item-path}
    GET /_api/v2.0/drives/{drive-id}/items/root:/{item-path}

    2- The first Send An HTTP Request to SharePoint will return a "@content.downloadUrl" value inside the JSON response that looks like a url with a bunch of things added like an temporary activation token and etc. You need to process that url to extract everything after your SharePoint url to use it as a new Uri in a second Send An HTTP Request to SharePoint you will make to finally extract the File Contet.

    Bellow is the expression I used to extract the useful part of the "@content.downloadUrl" value:

    substring(
        body('SendAnHTTPrequestToSharePoint-GET-FileOrFolder-UsingPath')?['@content.downloadUrl'],
        add(
            length(
                outputs('SiteURL')
            ),
            1
        )
    )
    

    Expression Details: SendAnHTTPrequestToSharePoint-GET-FileOrFolder-UsingPath is the name of the first HTTP action i used and SiteURL is the URL for the website i had stored as an output of a previous action. I added 1 to the lenght for the substring so i could get the forward slash '/' after the site url. You can change this expression with your preferences.

    3- After extracting the uri i used that outputed string to inside the second Send An HTTP Request to SharePoint action and output of that will be the file content.

    You can now save that to a sharepoint, one drive, attach to an email, list or do whatever you need with it.

    Hope this is useful for someone. I still am clueless about why /content doesn't work and what is going on with all the redirect error point to the same url. If you know why, please teach me.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.