Overview
ThehttpGetJson utility function provides a simple way to make HTTP/HTTPS GET requests and retrieve JSON data or other content types. It handles both HTTP and HTTPS protocols automatically and includes built-in error handling.
Function Signature
Parameters
The complete URL to fetch data from. Must include the protocol (http:// or https://)
Optional configuration object
Return Value
Returns aPromise that resolves with:
- Parsed JSON object if the content type is
application/json(default) - Raw string data if a different content type is specified via
headers.expectedContentType undefinedif an error occurs andsuppressErrorsis set totrue- Rejected promise with an error if the request fails and
suppressErrorsisfalse
Usage Examples
Basic JSON Request
With Error Suppression
Custom Headers and Content Type
With Custom Agent
Error Handling
The function validates responses and rejects with errors in the following cases:Status Code Errors
Status Code Errors
If the response status code is not
200, the promise is rejected with an error message:Content Type Mismatch
Content Type Mismatch
If the response content type doesn’t match the expected type, the promise is rejected:The default expected content type is
application/json.Network Errors
Network Errors
Network-level errors (connection refused, DNS errors, etc.) are caught and either:
- Rejected if
suppressErrorsisfalse(default) - Resolved with
undefinedifsuppressErrorsistrue
HTTPS Handling
Common Use Cases
Fetch API Data
Retrieve JSON data from REST APIs during the Scully build process
Content Retrieval
Fetch remote content like RSS feeds, XML sitemaps, or configuration files
Plugin Development
Use in custom Scully plugins to fetch data for route generation
Optional Resources
Load optional resources with error suppression for graceful fallbacks
Related
Route Discovery
Learn how to use httpGetJson in route discovery
Router Plugins
Create router plugins that fetch remote data
Source Code
Location:libs/scully/src/lib/utils/httpGetJson.ts:14
