Invoke-webrequest didn't return 404 for unvalid page url

g h 716 Reputation points
2020-08-11T06:11:30.943+00:00

As stated in the title, if I had the following code to test a SharePoint url if it's valid:

$req = Invoke-WebRequest -Uri "http://sp/404"
$stat = $req.StatusCode

It's always returning 200 as status code.

It's wired as the page url is not valid and can't be open in the browser.

Can you explain the reason for this ?

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,608 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jerryzy 10,571 Reputation points
    2020-08-12T01:28:08.487+00:00

    Hi @g h ,

    When accessing a invalid SharePoint page url, it will redirect to a page url like this:

    http://sp/Pages/PageNotFoundError.aspx?requestUrl=http://sp/404

    This shoule be a default action in SharePoint to handle invalid page url and this page request is 200 when checking in Developer Tool:

    17028-snipaste-2020-08-12-09-27-32.png

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Trevor Seward 11,701 Reputation points
    2020-08-11T14:58:54.287+00:00

    This will occur if you make a request to a managed path endpoint and is expected behavior.

    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.