Repository: localstack/localstack

Issue:
bug: REST ApiGateway responses header content-type always text/html
### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current Behavior

I Created an REST APIGatway with lambda handler using AWS CDK. when I hit the API the response header content type is always  set to "text/html"  instead of `application/json` . even if I changed the response template or added the header to lambda returned response. when I deploy the same stack on AWS account  the response header is fine and work as expected

### Expected Behavior

Response Header content-type to be `application/json`

### How are you starting LocalStack?

  `localstack start` command

### Steps To Reproduce

#### How are you starting localstack (e.g., `bin/localstack` command`)

    `localstack start` command

#### Client commands (e.g., AWS SDK code snippet, or sequence of "awslocal" commands)

I'm using AWS CDK typescript application and deploy it using `cdklocal` command on localstack

Using CDK follow the steps:
- create  REST ApiGateway
- create Lambda function 
- integrate lambda with APIGatway

On Command line:
- deploy using `cdklocal deploy` command

Finally
- test the endpoint and check the response header content-type




### Environment

```markdown
- OS: Windows 
- LocalStack: latest
```


### Anything else?

When I deploy the same stack on AWS account everything works fine and the response header content-type is `application/json`


Discussion:
Welcome to LocalStack! Thanks for reporting your first issue and our team will be working towards fixing the issue for you or reach out for more background information. We recommend joining our [Slack Community](https://localstack.cloud/contact/) for real-time help and drop a message to LocalStack Pro Support if you are a Pro user! If you are willing to contribute towards fixing this issue, please have a look at our [contributing guidelines](https://github.com/localstack/.github/blob/main/CONTRIBUTING.md) and our [developer guide](https://docs.localstack.cloud/developer-guide/).
I don't exactly have the same behavior ! 

I'm totally agree the fact that the default behavior (a.k.a if you don't provide any Content-Type) is different on localstack and on a real AWS account when you respond with json data. The default on localstack seems to be "text/html" while it is "application/json" on an AWS account.

But when I set the header manually, it works as expected (on localstack and on an AWS account). Maybe an issue with your programming language ? (I did it in NodeJS)

```nodejs
const response = {
    statusCode: status,
    body: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  };
```

Nevertheless, still annoying to add it manually so that it works on localstack... 

Yes, while triaging this issue I figure out that it the integration works as intended just that localstack doesn't validate the response as AWS explains [here](https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/). 

So the work I'm doing for this issue is this:
- add a test that confirms that the integration returns the headers that the lambda defines.
- add the validation for the response like AWS does and a test for that.
- and now, add the default behavior that @avanmalleghem mentions.
 
It's taking some time because I'm stuck with some internal utilities for testing.
Hi @salah-mueller!
This should be fixed with #6170 which has just been merged. The fix should already be contained in the `localstack/localstack:latest` image. Is there a chance you could verify the fix? Thanks!
Hello @alexrashed, looks like it doesn't work ! I still have "text/html; charset=utf-8" as default Content-Type header.

Resolving patch:
diff --git a/localstack/services/apigateway/integration.py b/localstack/services/apigateway/integration.py
--- a/localstack/services/apigateway/integration.py
+++ b/localstack/services/apigateway/integration.py
@@ -197,6 +197,7 @@ def invoke(self, invocation_context: ApiInvocationContext):
             response = result
         else:
             response = LambdaResponse()
+            response.headers.update({"content-type": "application/json"})
             parsed_result = result if isinstance(result, dict) else json.loads(str(result or "{}"))
             parsed_result = common.json_safe(parsed_result)
             parsed_result = {} if parsed_result is None else parsed_result
@@ -208,7 +209,6 @@ def invoke(self, invocation_context: ApiInvocationContext):
                     'Lambda output should follow the next JSON format: { "isBase64Encoded": true|false, "statusCode": httpStatusCode, "headers": { "headerName": "headerValue", ... },"body": "..."}'
                 )
                 response.status_code = 502
-                response.headers.update({"content-type": "application/json"})
                 response._content = json.dumps({"message": "Internal server error"})
                 return response
 
diff --git a/localstack/services/apigateway/invocations.py b/localstack/services/apigateway/invocations.py
--- a/localstack/services/apigateway/invocations.py
+++ b/localstack/services/apigateway/invocations.py
@@ -382,7 +382,6 @@ def invoke_rest_api_integration_backend(invocation_context: ApiInvocationContext
                 response = result
             else:
                 response = LambdaResponse()
-                response.headers.update({"content-type": "application/json"})
 
                 parsed_result = (
                     result if isinstance(result, dict) else json.loads(str(result or "{}"))


Test patch:
diff --git a/tests/integration/awslambda/test_lambda_integration.py b/tests/integration/awslambda/test_lambda_integration.py
--- a/tests/integration/awslambda/test_lambda_integration.py
+++ b/tests/integration/awslambda/test_lambda_integration.py
@@ -481,6 +481,8 @@ def test_malformed_response_apigw_invocation(self, create_lambda_function, lambd
         result = safe_requests.get(url)
 
         assert result.status_code == 502
+        assert result.headers.get("Content-Type") == "application/json"
+        assert json.loads(result.content)["message"] == "Internal server error"
 
 
 class TestKinesisSource: