Azure/azure-functions-python-worker

Body formatted when 'application/json' causing incorrect Content-Length

Closed this issue · 2 comments

HttpRequest's get_body() method returns bytes and should therefore be unformatted. When Content-Type is 'application/json' the body is formatted (adding CRLF characters) which can break certain functionality. This is especially problematic for source which depends on the Content-Length header, which remains unmodified from the original request. Implementations needing to parse the bytes themselves depend on the length of the entity body matching the Content-Length header.

Repro steps

Provide the steps required to reproduce the problem:

  1. Create an httpTrigger function in python similar to:
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse
    assert str(len(req.get_body())) == req.headers.get('Content-Length'), "Body does not match Content-Length"
    return "Worked"
  1. Perform a POST request with some nesting objects or arrays similar to:
curl -X POST "http://localhost:7071/app/" -H  "Content-Type: application/json" -d "{\"user\": {\"firstName\": \"John\",    \"lastName\": \"Doe\"}}"

Expected behavior

Returns "Worked" and does not assert.

Actual behavior

Assert and 500 is returned.

Known workarounds

Don't set "Content-Type: application/json" and they will match. But this is terrible for many other reasons and we shouldn't ask clients to do this.

Related information

requirements.txt

azure-functions==1.0.0a5
azure-functions-worker==1.0.0a6
grpcio==1.14.2
grpcio-tools==1.14.2

Problem exists both in local and hosted Azure environments, but

> func --version
2.3.199

@pragnagopa This looks like a host issue. Is it possible to avoid JSON reformatting of request bodies?