AngularJS $http POST default Content‑Type header – version‑dependent behavior
0 reputation · 17 Nov 2025, 12:55 UTC
0 reputation · 17 Nov 2025, 12:55 UTC
The $http service’s default Content-Type for POST requests changes between AngularJS releases. In 1.4.x the default is application/x-www-form-urlencoded, while 1.5.x and later switch to application/json. This shift is not prominently documented in migration guides, leaving developers uncertain which header is applied in a given project.
When upgrading, code that expects the legacy form‑urlencoded format may send malformed JSON to the server, causing validation failures or unexpected errors. Third‑party modules that rely on the older default may also break unless explicitly reconfigured.
Key constraints: the default header cannot be overridden globally without altering $httpProvider.defaults.headers.post; runtime detection of the AngularJS version is required to decide which default applies; and maintaining backward compatibility across versions is critical for API endpoints that expect key/value pairs.
What is the exact default Content-Type for POST requests in AngularJS 1.4.x versus 1.5.x and newer? How can a project reliably determine the running AngularJS version at runtime to apply the appropriate header? What is the recommended strategy to preserve compatibility when upgrading from 1.4.x to 1.5.x or later?
34775 reputation · 17 Nov 2025, 15:02 UTC
The default Content-Type header for $http.post() requests differs based on the AngularJS version:
application/x-www-form-urlencoded.application/json.To determine the loaded version of AngularJS at runtime, access the angular.version.full property. This provides a string representation of the current release.
var currentVersion = angular.version.full;
console.log('Running AngularJS version: ' + currentVersion);
When migrating from 1.4.x to 1.5.x+, you can preserve legacy behavior using one of the following two methods, depending on whether the requirement is global or request-specific.
To force all POST requests to use the legacy form-urlencoded format, configure the $httpProvider during the application configuration phase. This prevents the need for runtime version checks across multiple controllers.
app.config(function($httpProvider) {
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
});
If your application communicates with both legacy (form-based) and modern (JSON-based) endpoints, explicitly define the header in the request configuration object.
$http.post('/api/legacy-endpoint', data, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
To verify which header is being sent, use the browser's Developer Tools > Network Tab. Select the POST request and inspect the Request Headers section for the Content-Type key.
Diagnostic Note: If you are using a third-party interceptor or a wrapper service around $http, please specify if that wrapper modifies headers, as it may override the global $httpProvider settings.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.