Handle JSON payloads as strings in HTTP Connectors task
In this tutorial, you learn how to handle JSON payloads when you use anHTTP Connectors task in Application Integration.
Incorrectly treating the payload can cause APIs to misinterpret it, resulting in400 Bad Request errors. To ensure APIs receive and parse your JSON payload correctly, treat the JSON payload as a string and explicitly set theContent-Type header toapplication/json.
Before you begin
- Verify you can accessApplication Integration.
Select your Google Cloud project and complete the following tasks:
- Grant the following roles to the service account that you want to use to create the connection:
roles/secretmanager.viewerroles/secretmanager.secretAccessor
- Enable the following services:
secretmanager.googleapis.com(Secret Manager API)connectors.googleapis.com(Connectors API)
If you haven't enabled these services for your project, theCreate Connection page prompts you to enable them when you create the connection.
- Grant the following roles to the service account that you want to use to create the connection:
- Add and configure anHTTP Connectors task.
- Add and configure theData Transformer task to prepare the JSON payload and headers.
Handle JSON payloads as strings
To handle JSON payloads that contain special characters when you use anHTTP Connectors task, complete the following steps:
- Create a string variable for the JSON payload
- Map the string variable to the connector input body
- Add a custom Content-Type header
- Execute the HTTP Connectors task
Create a string variable for the JSON payload
- In the Google Cloud console, go to theApplication Integration page.
- In the navigation menu, clickIntegrations.
- Select an existing integration orcreate a new one. Ensure your integration includes the configuredHTTP Connectors andData Transformer tasks as detailed in theBefore you begin section.
- In theintegration editor, clickCreate to open theCreate variable dialog.
- In theCreate variable dialog:
- Enter the following information:
- Name: Enter a name, for example,
jsonPayloadString. - Data Type: SelectString.
- Default Value:
- If the JSON payload is populated by a preceding task in the integration flow, for example, anotherData Transformer task, leave this field empty. The variable will be populated dynamically based on the output of previous tasks in the integration.
- If the JSON payload is not populated by a preceding task, you must provide a static default JSON payload. Enter the JSON structure as a single string in this field.
Here are two examples of how to enter JSON strings in theDefault Value field:
Without special characters: To represent the JSON
{"name": "Developer", "key": "test"}, enter exactly:{ "name": "Developer", "key": "test" }.With special characters: When your JSON contains special characters like newline (
\n), you must escape them. For example, to represent the JSON{"key": "value1\nvalue2"}, enter{"key": "value1\\nvalue2"}in the field.
- Name: Enter a name, for example,
- ClickCreate.
For more information on creating and using variables, seeVariables. - Enter the following information:
Map the string variable to the connector input body
In theData Transformer Editor, map thejsonPayloadString variable to theconnectorInputPayload.Body field.
- In your integration, add aData Transformer task.
- Open theData Transformer Editor. You can use either theDiagram mode or theScript mode.
- To map the variable:
- Diagram mode: Add
jsonPayloadStringas an input and map it to theconnectorInputPayload.Body output field. - Script mode: Write Jsonnet code to assign the value:
connectorInputPayload.Body = vars.jsonPayloadString;.
- Diagram mode: Add
Add a custom Content-Type header
- In theData Transformer Editor, add a mapping for custom headers to theconnectorInputPayload.Headers field.
- Diagram mode: Add a constant input with the value
{"Content-Type": "application/json"}and map it toconnectorInputPayload.Headers. - Script mode: Add the following Jsonnet to set the header:
connectorInputPayload.Headers = { "Content-Type": "application/json"};
- Diagram mode: Add a constant input with the value
Execute the HTTP Connectors task
Before executing your integration, verify thatconnectorInputPayload.Body is populated by thejsonPayloadString variable and thatconnectorInputPayload.Headers includes theContent-Type: application/json header. This setup ensures theHTTP Connectors task sends the JSON payload as a string with the correctContent-Type in theData Transformer task.
Run your integration to test the API request. If the API returns a400 Bad Request error, verify the mappings forconnectorInputPayload.Body andconnectorInputPayload.Headers again in theData Transformer task.
What's next
Try building integrations with other connectors. For a list of all supported connectors, see Connector reference.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-19 UTC.