Method and Body Attributes


Method

The method attribute is a string that is used to specify the HTTP request method type.

Here is a list of some commonly used method types:

  • Get - used to retrieve an existing data resource
  • Head - used to retrieve HTTP headers
  • Post - used to create a new data resource
  • Put - used to create a new data resource or modify an existing data resource
  • Delete - used to delete a data resource


Notice how an init object with a method attribute of "Post" can be created:

let initObject = {
    method: 'POST'
}


Body

The body attribute is a JSON string used to send data along with a fetch request. If the body value is an object, it is important to stringify the object that is being sent using JSON.stringify() or it will not process correctly. Get and Head HTTP requests can not have bodies.


Notice how an init object with a body attribute representing an object can be created:

let myBody = {
    id: 12345,
    name: 'abc',
    age: 21
}

let initObject = {
    body: JSON.stringify(myBody)
}

results matching ""

    No results matching ""