JustBarnt
JustBarnt

Reputation: 124

Proper use case for Request VS Response with Fetch/Axios

I am learning to create REST API's and use HTTP requests. It seems though that naming conventions for Request/Response is not consistent.

What I mean by all that is, many things I google regarding fetch requests. Use response as the variable name for fetch. EX: const response = await fetch('Someurlhere.com');

But if I am understanding correctly, that would be the request would it not? Response should be const response = await request.json();

I am really just trying to make sure I am understanding everything correctly in my head of if there is a reason why there are so many resources that use my first example, which to me seems like an improper use and will just confuse new learners like myself.

Upvotes: 1

Views: 602

Answers (1)

Quentin
Quentin

Reputation: 943585

But if I am understanding correctly, that would be the request would it not?

No. The request is what calling fetch triggers the browser to send to the URL. You pass the data about what the request should look like as arguments to fetch.

You could do that indirectly (which is rarely useful) by passing a Request object as an argument to fetch

const request = new Request("http://example.com");
const response = await fetch(request);

… but fetch returns a promise that resolves as a Response.

Response should be const response = await request.json();

No. The promise returned from response.json() resolves as the result of parsing the response body as JSON.


You're confusing "the response" and "something inside the response" with "the request" and "the response"

Upvotes: 2

Related Questions