HTTP Connector 1.7 | MuleSoft Documentation
# HTTP Connector 1.7
Anypoint Connector for HTTP (HTTP Connector) enables you to declare HTTP servers that listen to requests and trigger flows, as well as HTTP clients that can communicate with any HTTP service. Additionally, the connector enables you to both expose and consume HTTP-based APIs. For example, APIkit and RESTConnect both use HTTP Connector internally to work with APIs.
HTTP Connector also supports HTTPS (TLS) connectivity, which provides easy ways to serve static resources and secure servers, and handles client authentication schemes.
For information about compatibility and fixed issues, see the HTTP Connector Release Notes.
## Before You Begin
To use this connector, you must be familiar with:
  * Anypoint Connectors
  * Mule runtime engine (Mule)
  * Elements and global elements in a Mule flow
  * How to create a Mule app using Anypoint Studio


Before creating an app, you must have access to Anypoint Platform and Anypoint Studio
## HTTP Servers
HTTP servers expose services for users or applications to consume. These servers receive data in the form of requests and return corresponding responses. The HTTP **Listener** source triggers flows when a request is received and responds after the flow finishes.
Declare an HTTP server by defining the listener source connection. This connection defines the host and port where requests are received.
The following example shows an XML example for an HTTP **Listener** source connection:
```
<http:listener-config name="serverConfig">
 <http:listener-connection host="localhost" port="8081"/>
</http:listener-config>
```

The connection configuration is `serverConfig`, which listens to a localhost address (IP address `127.0.0.1`) on port `8081`. After you set up a server in Anypoint Studio, you can access this server at the `http://localhost:8081` URL.
To fully configure your server, specify resource paths to execute a flow so that when a request matching that path is received, Mule runtime engine (Mule) runs your flow.
The following example shows the XML configuration for two HTTP **Listener** sources in different flows that execute depending on the path triggered:
```
<flow name="hello">
  <http:listener path="greet" config-ref="serverConfig"/>
  <set-payload value="Hello, world!"/>
</flow>
<flow name="bye">
  <http:listener path="exit" config-ref="serverConfig"/>
  <set-payload value="Goodbye, world!"/>
</flow>
```

Both listener sources use the connection configuration `serverConfig` declared in the first example. If the HTTP **Listener** source is triggered with `http://localhost:8081/greet`, the connector returns `Hello, world!`. If the source is triggered with `http://localhost:8081/exit`, the connector returns `Goodbye, world!`. The received HTTP request data is shared with the flow when executed. You can customize HTTP responses.
The following example shows an XML configuration for an HTTP **Listener** source that customizes the body of the HTTP responses:
```
<flow name="hello">
 <http:listener path="greet" config-ref="serverConfig">
  <http:response>
   <http:body>
    #[payload ++ ' I am an HTTP server.']
   </http:body>
  </http:response>
 </http:listener>
 <set-payload value="#['Hello, ' ++ (attributes.queryParams.name default 'world') ++ '!']" />
</flow>
```

If the HTTP **Listener** source is triggered with `http://localhost:8081/greet?name=Ana`, the connector returns `Hello, Ana! I am an HTTP server.`. If the source is triggered with `http://localhost:8081/greet`, the connector returns `Hello, world! I am an HTTP server.`.
For more information about the HTTP **Listener** source, refer to the Configure an HTTP Listener source documentation.
## HTTP Clients
HTTP clients enable you to send requests to HTTP services and manage their responses. The HTTP **Request** operation executes HTTP requests and returns the corresponding answers.
Although you don’t need to declare HTTP clients, declaring them enables you to reuse configurations. If you don’t declare an HTTP client, specify the entire URL each time you want to send a request to that URL. Also, if you make several requests on different API resources, declare the client to which the host connects.
The following example shows an XML configuration for an HTTP **Request** operation:
```
<http:request-config name="clientConfig">
 <http:request-connection host="localhost" port="8081"/>
</http:request-config>
```

The HTTP request configuration is `clientConfig`, and sends requests to `localhost` and port `8081`.
To perform requests, each operation must include the path of the request target. The following example shows an XML configuration for two HTTP **Request** operations with different paths:
```
<flow name="helloAndBye">
 <http:request method="GET" path="greet" config-ref="clientConfig"/>
 <logger level="INFO" message="#[payload]"/>
 <http:request method="GET" path="exit" config-ref="clientConfig"/>
 <logger level="INFO" message="#[payload]"/>
</flow>
```

The service exposed is consumed by sending requests to `greet` and `exit`, and when the flow executes, it displays:
```
Hello, world! I am an HTTP server.
Goodbye, world!
```

You can customize HTTP requests to share the response data with the flow. The following example shows an HTTP **Request** operation XML configuration that customizes the request sent with query parameters and uses the received HTTP response status:
```
<flow name="fullHello">
 <http:request method="GET" path="greet" config-ref="clientConfig">
  <http:query-params>
   #[{"name" : "HTTP client"}]
  </http:query-params>
 </http:request>
 <logger level="INFO"
  message="#['Received a ' ++ attributes.statusCode ++ ' response with body: {' ++ payload ++ '}']"/>
</flow>
```

When the flow executes, it displays:
```
Received a 200 response with body: {Hello, HTTP client! I am an HTTP server.}
```

For further information about the HTTP **Request** operation, refer to Configure HTTP Request operation documentation.
## Next Step
After you complete the prerequisites, you are ready to create an app and configure the connector using Anypoint Studio.
## See Also
  * Introduction to Anypoint Connectors
  * Use Exchange to Discover Connectors, Templates, and Examples
  * HTTP Connector Reference
  * MuleSoft Help Center