Invoke-WebRequest
command in PowerShell to make HTTP requests, the command typically returns HTTP status codes that indicate the result of the request. Here are the common HTTP status codes and their meanings:
1xx: Informational
- 100 Continue: The initial part of a request has been received and has not yet been rejected by the server.
- 101 Switching Protocols: The server is switching protocols as requested by the client.
2xx: Success
- 200 OK: The request has succeeded.
- 201 Created: The request has been fulfilled, and a new resource has been created.
- 202 Accepted: The request has been accepted for processing, but the processing is not complete.
- 204 No Content: The server successfully processed the request, but there is no content to return.
3xx: Redirection
- 301 Moved Permanently: The requested resource has been assigned a new permanent URI.
- 302 Found: The requested resource resides temporarily under a different URI.
- 304 Not Modified: Indicates that the resource has not been modified since the version specified by the request headers.
4xx: Client Errors
- 400 Bad Request: The server could not understand the request due to invalid syntax.
- 401 Unauthorized: The request requires user authentication.
- 403 Forbidden: The server understood the request but refuses to authorize it.
- 404 Not Found: The server could not find the requested resource.
- 405 Method Not Allowed: The request method is not supported for the requested resource.
- 429 Too Many Requests: The user has sent too many requests in a given amount of time (“rate limiting”).
5xx: Server Errors
- 500 Internal Server Error: The server encountered an unexpected condition that prevented it from fulfilling the request.
- 501 Not Implemented: The server does not support the functionality required to fulfill the request.
- 502 Bad Gateway: The server received an invalid response from the upstream server.
- 503 Service Unavailable: The server is not ready to handle the request, often due to overload or maintenance.
- 504 Gateway Timeout: The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.
You can capture the status code of the HTTP response in PowerShell by checking the StatusCode
property of the response object. Here’s an example of how you might do that:
$response = Invoke-RestMethod -Uri "https://example.com/api" -Method Get
$statusCode = $response.StatusCode
Write-Output "Status Code: $statusCode"
If the request fails or the server returns an error code (4xx or 5xx), PowerShell will typically throw an exception unless handled withtry
/catch
.
0 Comments