# Handle JSON payloads as strings in HTTP Connectors task

In this tutorial, you learn how to handle JSON payloads when you use an **HTTP Connectors** task in Application Integration.

Incorrectly treating the payload can cause APIs to misinterpret it, resulting in `400 Bad Request` errors. To ensure APIs receive and parse your JSON payload correctly, treat the JSON payload as a string and explicitly set the `Content-Type` header to `application/json`.

## Before you begin

- Verify you can access [Application Integration](https://docs.cloud.google.com/application-integration/docs/setup-application-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.viewer`
    - `roles/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, the **Create Connection** page prompts you to enable them
    when you create the connection.
- Add and configure an [HTTP Connectors](https://docs.cloud.google.com/application-integration/docs/configure-connectors-task) task.
- Add and configure the [Data Transformer task](https://docs.cloud.google.com/application-integration/docs/configure-data-transformer-script-task) to prepare the JSON payload and headers.

## Handle JSON payloads as strings

To handle JSON payloads that contain special characters when you use an **HTTP Connectors** task, complete the following steps:

- [Create a string variable for the JSON payload](https://docs.cloud.google.com/application-integration/docs/json-in-subtask#create-string-variable)
- [Map the string variable to the connector input body](https://docs.cloud.google.com/application-integration/docs/json-in-subtask#map-string-variable)
- [Add a custom Content-Type header](https://docs.cloud.google.com/application-integration/docs/json-in-subtask#add-content-type-header)
- [Execute the HTTP Connectors task](https://docs.cloud.google.com/application-integration/docs/json-in-subtask#execute-connector-task)

### Create a string variable for the JSON payload

1. In the Google Cloud console, go to the **Application Integration** page.

   [Go to Application Integration](https://console.cloud.google.com/integrations)
2. In the navigation menu, click **Integrations**.
3. Select an existing integration or [create a new one](https://docs.cloud.google.com/application-integration/docs/setup-application-integration). Ensure your integration includes the configured **HTTP Connectors** and **Data Transformer** tasks as detailed in the [Before you begin](https://docs.cloud.google.com/application-integration/docs/json-in-subtask#before-you-begin) section.
4. In the *integration editor*, click **Create** to open the **Create variable** dialog.
5. In the **Create variable** dialog:
   1. Enter the following information:
      1. **Name:** Enter a name, for example, `jsonPayloadString`.
      2. **Data Type:** Select **String**.
      3. **Default Value:**
         - If the JSON payload is populated by a preceding task in the integration flow, for example, another [Data Transformer](https://docs.cloud.google.com/application-integration/docs/configure-data-transformer-script-task) 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 the **Default 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.
   2. Click **Create**.

   ![Create a string variable for the JSON payload](https://docs.cloud.google.com/static/application-integration/images/string.png)
For more information on creating and using variables, see [Variables](https://docs.cloud.google.com/application-integration/docs/variables).

### Map the string variable to the connector input body

In the **Data Transformer Editor** , map the `jsonPayloadString` variable to the **connectorInputPayload.Body** field.

1. In your integration, add a [Data Transformer task](https://docs.cloud.google.com/application-integration/docs/configure-data-transformer-script-task).
2. Open the **Data Transformer Editor** . You can use either the **Diagram mode** or the **Script mode**.
3. To map the variable:
   - **Diagram mode:** Add `jsonPayloadString` as an input and map it to the **connectorInputPayload.Body** output field.
   - **Script mode:** Write Jsonnet code to assign the value: `connectorInputPayload.Body = vars.jsonPayloadString;`.

### Add a custom Content-Type header

1. In the **Data Transformer Editor** , add a mapping for custom headers to the **connectorInputPayload.Headers** field.
   - **Diagram mode:** Add a constant input with the value `{"Content-Type": "application/json"}` and map it to **connectorInputPayload.Headers**.
   - **Script mode:** Add the following Jsonnet to set the header:

     ```
     connectorInputPayload.Headers = {
       "Content-Type": "application/json"
     };
             
     ```

### Execute the HTTP Connectors task

Before executing your integration, verify that [`connectorInputPayload.Body`](https://docs.cloud.google.com/application-integration/docs/json-in-subtask#map-string-variable) is populated by the [`jsonPayloadString`](https://docs.cloud.google.com/application-integration/docs/json-in-subtask#map-string-variable) variable and that [`connectorInputPayload.Headers`](https://docs.cloud.google.com/application-integration/docs/json-in-subtask#add-content-type-header) includes the [`Content-Type: application/json` header](https://docs.cloud.google.com/application-integration/docs/json-in-subtask#add-content-type-header). This setup ensures the **HTTP Connectors** task sends the JSON payload as a string with the correct `Content-Type` in the **Data Transformer** task.

Run your integration to test the API request. If the API returns a `400 Bad Request` error, verify the mappings for `connectorInputPayload.Body` and `connectorInputPayload.Headers` again in the **Data Transformer** task.

## What's next

Try building integrations with other connectors. For a list
of all supported connectors,
see [Connector reference](https://docs.cloud.google.com/integration-connectors/docs/connector-reference-overview).