Making a request

Several Options:

  • axios

  • requests

  • fetch(HTML5?)

  • raw XMLhttprequest(native)

  • jquery

Axios

Promise based HTTP client, best

const axios = require('axios');
import axios from 'axios';

//GET
axios.get("/event", { params: { api_key })
  .then(response => {
    console.log(response.data.url);
  })
  .catch(error => {
    console.log(error);
  });

//POST
axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

axios.get(url[, config])

axios.post(url[, data[, config]])

Error Handling

  • err.response.status if any other server status than 2xx

  • err.request if request was made without a response

  • err.message === "Network Error" special network error casearrow-up-right

  • err.message if something while setting up request

Auth Bearer

Setting up base

Multiple Requests

Sending Blob

Advanced Axios

Some random code I found to test out

Requests

Simplified version of python

Fetch

Builtin to modern browsers, adds parse step compared to axios and some other gotchas

Response.status

.json()

.text()

Advanced Fetch

Async await

Creating URL Queries

OG Fundamental

Parse JSON Response

Could also send and receive XML, but nahhhhh JSON.parse(http.responseText); JSON.parse is only avaliable in most modern browsers, could use lib like jQuery

XMLHttpRequest States

  • 0 request not initialized

  • 1 request set up

  • 2 request has been sent

  • 3 request is in process

  • 4 request is complete //essential

In JQuery

Last updated