---
page_title: "http Data Source - terraform-provider-http"
subcategory: ""
description: |-
  The http data source makes an HTTP GET request to the given URL and exports
  information about the response.
  The given URL may be either an http or https URL. At present this resource
  can only retrieve data from URLs that respond with text/* or
  application/json content types, and expects the result to be UTF-8 encoded
  regardless of the returned content type header.
  ~> Important Although https URLs can be used, there is currently no
  mechanism to authenticate the remote server except for general verification of
  the server certificate's chain of trust. Data retrieved from servers not under
  your control should be treated as untrustworthy.
---

# http (Data Source)

The `http` data source makes an HTTP GET request to the given URL and exports
information about the response.

The given URL may be either an `http` or `https` URL. At present this resource
can only retrieve data from URLs that respond with `text/*` or
`application/json` content types, and expects the result to be UTF-8 encoded
regardless of the returned content type header.

~> **Important** Although `https` URLs can be used, there is currently no
mechanism to authenticate the remote server except for general verification of
the server certificate's chain of trust. Data retrieved from servers not under
your control should be treated as untrustworthy.

## Example Usage

```terraform
# The following example shows how to issue an HTTP GET request supplying
# an optional request header.
data "http" "example" {
  url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

  # Optional request headers
  request_headers = {
    Accept = "application/json"
  }
}

# The following example shows how to issue an HTTP HEAD request.
data "http" "example_head" {
  url    = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
  method = "HEAD"
}

# The following example shows how to issue an HTTP POST request
# supplying an optional request body.
data "http" "example_post" {
  url    = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
  method = "POST"

  # Optional request body
  request_body = "request body"
}
```

## Usage with Postcondition

[Precondition and Postcondition](https://www.terraform.io/language/expressions/custom-conditions)
checks are available with Terraform v1.2.0 and later.

```terraform
data "http" "example" {
  url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

  # Optional request headers
  request_headers = {
    Accept = "application/json"
  }

  lifecycle {
    postcondition {
      condition     = contains([201, 204], self.status_code)
      error_message = "Status code invalid"
    }
  }
}
```

## Usage with Precondition

[Precondition and Postcondition](https://www.terraform.io/language/expressions/custom-conditions)
checks are available with Terraform v1.2.0 and later.

```terraform
data "http" "example" {
  url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

  # Optional request headers
  request_headers = {
    Accept = "application/json"
  }
}

resource "random_uuid" "example" {
  lifecycle {
    precondition {
      condition     = contains([201, 204], data.http.example.status_code)
      error_message = "Status code invalid"
    }
  }
}
```

## Usage with Provisioner

[Failure Behaviour](https://www.terraform.io/language/resources/provisioners/syntax#failure-behavior)
can be leveraged within a provisioner in order to raise an error and stop applying.

```terraform
data "http" "example" {
  url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

  # Optional request headers
  request_headers = {
    Accept = "application/json"
  }
}

resource "null_resource" "example" {
  # On success, this will attempt to execute the true command in the
  # shell environment running terraform.
  # On failure, this will attempt to execute the false command in the
  # shell environment running terraform.
  provisioner "local-exec" {
    command = contains([201, 204], data.http.example.status_code)
  }
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `url` (String) The URL for the request. Supported schemes are `http` and `https`.

### Optional

- `ca_cert_pem` (String) Certificate data of the Certificate Authority (CA) in [PEM (RFC 1421)](https://datatracker.ietf.org/doc/html/rfc1421) format.
- `insecure` (Boolean) Disables verification of the server's certificate chain and hostname. Defaults to `false`
- `method` (String) The HTTP Method for the request. Allowed methods are a subset of methods defined in [RFC7231](https://datatracker.ietf.org/doc/html/rfc7231#section-4.3) namely, `GET`, `HEAD`, and `POST`. `POST` support is only intended for read-only URLs, such as submitting a search.
- `request_body` (String) The request body as a string.
- `request_headers` (Map of String) A map of request header field names and values.

### Read-Only

- `body` (String, Deprecated) The response body returned as a string. **NOTE**: This is deprecated, use `response_body` instead.
- `id` (String) The URL used for the request.
- `response_body` (String) The response body returned as a string.
- `response_headers` (Map of String) A map of response header field names and values. Duplicate headers are concatenated according to [RFC2616](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2).
- `status_code` (Number) The HTTP response status code.