[$http:badreq] The request is not properly formatted. $http transformRequest default in AngularJS 1.6
0 reputation · 11 Jul 2024, 14:16 UTC
0 reputation · 11 Jul 2024, 14:16 UTC
AngularJS 1.6 changed the default $http transformRequest behavior for plain JavaScript objects. The documented change means requests are JSON stringified where earlier releases used form-encoded serialization. This creates compatibility uncertainty with backends that still expect form-encoded payloads.
When a backend that expects form-encoded data receives JSON, the response may be a client error with a non-JSON body. $http's transformResponse attempts to parse responses as JSON, and under specific conditions the service surfaces the error string "[$http:badreq] The request is not properly formatted.". The decision to keep the new default without a documented opt-out remains an open design question.
What response status and body conditions cause $http to emit [$http:badreq] The request is not properly formatted. in AngularJS 1.6? Is the default transformRequest change configurable via $httpProvider without providing a custom serialization function? Does the error originate from transformResponse parsing failure or from request formatting validation?
34775 reputation · 11 Jul 2024, 21:47 UTC
$httpParamSerializerJQLike service via $httpProvider.defaults.transformRequest (global) or per-request config. No custom serialization function is required.defaultTransformResponse when JSON.parse fails on a 2xx response.The string [$http:badreq] The request is not properly formatted. suggests a request problem, but AngularJS 1.6 source (src/ng/http.js) shows it is raised in the default transformResponse function:
function defaultTransformResponse(data, headersGetter) { if (isString(data)) { try { data = JSON.parse(data); } catch (e) { throw minErr('$http')('badreq', 'The request is not properly formatted.'); } } return data;}It catches JSON.parse failures only for 2xx responses. A backend that returns a 400/500 HTML page with a 2xx status, or an empty 200 body, triggers this exact error.
AngularJS 1.6 changed the default transformRequest to JSON-stringify plain objects. The documented opt-out is the built-in $httpParamSerializerJQLike service, which reproduces the pre-1.6 application/x-www-form-urlencoded behavior.
angular.module('app').config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.transformRequest = [$httpParamSerializerJQLike];}]);$http({ method: 'POST', url: '/api/endpoint', data: { key: 'value' }, transformRequest: $httpParamSerializerJQLike, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }});Note: setting the Content-Type header explicitly is recommended when overriding transformRequest so the backend knows how to parse the payload.
responseType: 'text' to the request config — this bypasses JSON parsing and lets you inspect the raw response body.{}) on success, or returns a proper 4xx/5xx status for errors.What is the exact HTTP status code and Content-Type header of the response that produces this error? (This determines whether the fix is backend-side, frontend transformRequest configuration, or adding responseType: 'text' for a known non-JSON endpoint.)
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.