V
V
Vladislav2020-09-30 19:32:48
API
Vladislav, 2020-09-30 19:32:48

How to implement imitation of getting data via API?

Good afternoon,
How to simulate receiving data via API, if only a json file is given in the test task. I don't quite understand what they want from me.

spoiler
{
"response": [
{
"id": 1,
"attributes": {
"title": "Flat",
"rooms": 3,
}
}
]
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vovash, 2020-09-30
@Air_O

simulate get request to json

export default class GetService{
    _apiBase = 'http://localhost:3000';

    async getResource(url) {
        const res = await fetch(`${this._apiBase}${url}`);
        if (!res.ok) {
            throw new Error(`Could not fetch ${url}` + 
                `, received ${res.status}`);
        }
        return await res.json();
    }

and how url feed him the path to json'y;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question