Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json content-type header to google_cloud_scheduler_job json examples #6229

Conversation

modular-magician
Copy link
Collaborator

Originally posted to the Terraform hashicorp/terraform-provider-google repo: hashicorp/terraform-provider-google#15754

Replaces GoogleCloudPlatform/magic-modules#8890 which was an attempt to move it to this repo but code changes were applied incorrectly (and only to 1 out of 2 files).

Context:

To add more context:
I did further verification using new Terraform resources on a simple project, to simulate this change:

# before (as-is without content-type header)
resource "google_cloud_scheduler_job" "scheduler_test_default_content_type" {
  name             = "scheduler-test-default-content-type"
  description      = "test http job - variant 1 - without content-type header"
  region           = local.region
  schedule         = "0 1 * * *"
  time_zone        = "America/New_York"
  attempt_deadline = "15s"

  http_target {
    http_method = "POST"
    uri         = local.test_function_url
    body        = base64encode("{\"foo\":\"bar\"}")

    oidc_token {
      service_account_email = local.default_compute_sa
    }
  }
}

# after (with content-type header)
resource "google_cloud_scheduler_job" "scheduler_test_default_content_type_2" {
  name             = "scheduler-test-default-content-type-2"
  description      = "test http job - variant 2 - with content-type header"
  region           = local.region
  schedule         = "0 2 * * *"
  time_zone        = "America/New_York"
  attempt_deadline = "15s"

  http_target {
    http_method = "POST"
    uri         = local.test_function_url
    body        = base64encode("{\"foo\":\"bar\"}")
    headers = {
      "Content-Type" = "application/json"
    }

    oidc_token {
      service_account_email = local.default_compute_sa
    }
  }
}

With the following Cloud Function (gen2) code:

import functions_framework

@functions_framework.http
def hello_http(request):
    content_type = request.headers.get('Content-Type')
    print(f'Content-Type: {content_type}')

    body = request.get_data(as_text=True)
    print(f'Request Body: {body}')

    request_json = request.get_json(silent=True)
    print(f"request_json:   {request_json}")
    
    # raises 415 status code (Unsupported Media Type) when called from "before", with application/octet-stream content type
    # request_json_2 = request.get_json(silent=False)

    if request_json and 'foo' in request_json:
        foo = request_json['foo']
    else:
        foo = 'Unknown'

    resp = 'foo: {}!'.format(foo)
    print(f"Returning {resp}")

    return resp

The results:

  • log for gcloud scheduler jobs run scheduler-test-default-content-type --project=[...] --location=[...]
Content-Type: application/octet-stream
Request Body: {"foo":"bar"}
request_json: None
Returning foo: Unknown!
  • log for gcloud scheduler jobs run scheduler-test-default-content-type-2 --project=[...] --location=[...]
Content-Type: application/json
Request Body: {"foo":"bar"}
request_json: {'foo': 'bar'}
Returning foo: bar!

Besides this example above, google docs for scheduler HttpMethod (docs) explicitly state for headers:

Content-Type: This will be set to "application/octet-stream". You can override this default by explicitly setting Content-Type to a particular media type when creating the job. For example, you can set Content-Type to "application/json"

So in case of example in the terraform docs that uses JSON body, the Content-Type should be adjusted accordingly.

Release Note Template for Downstream PRs (will be copied)

Derived from GoogleCloudPlatform/magic-modules#8892

@modular-magician modular-magician merged commit afdbcb8 into hashicorp:main Sep 8, 2023
@modular-magician modular-magician deleted the downstream-pr-17b8723fe0ee8cfcbd7de6434226d776e4d54fcf branch November 16, 2024 02:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant