-
Notifications
You must be signed in to change notification settings - Fork 54
Description
When a function is invoked via an HTTP trigger, the $HttpRequest.Body property type depends on the actual request body content. For example, when the request body contains a simple string like Hello, $HttpRequest.Body will contain 'Hello' as a string. However, if the request body contains anything that could be interpreted as a syntactically valid JSON (e.g. {"Hello":"world"}), $HttpRequest.Body will contain the result of JSON deserialization (@{ Hello = 'world' } as a hashtable), even when the request explicitly specifies Content-Type: text/plain. This implicit JSON deserialization may be convenient in many cases, but will cause confusion and unexpected function behavior changes depending on the client input.
Consider following more deterministic rules, such as:
- If the request has
Content-Type: application/jsonexplicitly specified:- If the body contains a valid JSON, deserialize and assign the result to
$HttpRequest.Body - If the body does not contain a valid JSON, respond with 400 (BadRequest) and don't invoke the function code
- If the body contains a valid JSON, deserialize and assign the result to
- If the request has
Content-Type: text/plainexplicitly specified:- Pass the body to
$HttpRequest.Bodyas is, don't even verify JSON syntax or attempt to deserialize
- Pass the body to
- etc.