Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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();

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading