Skip to main content

HTTP client

What you'll learn
  • Configuring your action factory to only make HTTP request
  • Using Foscia as an HTTP client
info

If you are using JSON REST or JSON:API blueprints, you can already use your action factory as an HTTP client.

Creating the action factory

Through CLI

You can get started for Foscia as an HTTP client using the Foscia CLI. It is available on any project where Foscia package is installed.

# Initialize Foscia and action factory inside "src/api" directory.
foscia init src/http --usage=http

Manually

You can use Foscia as a simple HTTP client (just like Axios). For this, you can configure an HTTP client action factory.

action.js
import { makeHttpClient } from 'foscia/blueprints';

const { action } = makeHttpClient({
baseURL: 'https://example.com',
});

export default action;

Using the HTTP client features

Once your action factory is ready, sending HTTP request is pretty easy:

import { makeGet, makePost } from 'foscia/http';
import { action } from './action';

// GET https://example.com/
const response = await action()
.use(makeGet('/'))
.run(raw());

// GET https://example.com/api/posts (and get JSON payload)
const data = await action()
.use(makePost('/api/posts', {
data: { title: 'Hello World!' },
}))
.run(raw((response) => response.json()));
Read the HTTP' enhancers API guide

Implementation notes

You may learn more about the HTTP adapter capabilities and implementation on the HTTP implementation guide.