# @compass/http

This package provides simplified HTTP request functionalities, allowing you to make requests using different methods (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS).

### Usage

Import it in the action:

```javascript
import http from "@compass/http";
```

### API Reference

#### `http.get(url: string, options?: RequestConfig)`

Sends a GET request to the specified URL.

* `url` (required, string): The URL where the request should be sent.
* `options` (optional, `RequestConfig`): The request configuration options.

Returns a Promise that resolves with the response.

#### `http.post(url: string, data?: any, options?: RequestConfig)`

Sends a POST request to the specified URL.

* `url` (required, string): The URL where the request should be sent.
* `data` (optional, any): The data to be sent as the request body.
* `options` (optional, `RequestConfig`): The request configuration options.

Returns a Promise that resolves with the response.\
\
`RequestConfig`

An object that configures the request. It accepts the following properties:

* `url` (optional, string): The URL where the request should be sent.
* `method` (optional, 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS'): The HTTP method for the request.
* `headers` (optional, object): Any headers you want to add to your request.
* `params` (optional, any): URL parameters to be sent with the request.
* `data` (optional, any): The data to be sent as the request body. Used primarily with 'POST', 'PUT' and 'PATCH' requests.

### Examples

To send a GET request:

```javascript
import http from "@compass/http";

export default async function() {
    try {
        const data = await http.get('https://example.com/api/items');
        log(data);

        return {
            items: data,
        }
    } catch (error) {
        log(error);
    }
}

```

To send a POST request:

```javascript
import http from "@compass/http";

export default async function() {
    const postData = {
        name: 'John',
        email: 'john@example.com'
    };

    try {
        const data = await http.post('https://example.com/api/users', postData);
        log(data);

        return {
            user: data,
        }
    } catch (error) {
        log(error);
    }
}

```

Remember that error handling is important to take care of any potential errors that might occur during the request.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.compass.art/automations/custom-actions/compass-http.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
