WebApps 2

requester

requester is available in a client side context and is used to perform HTTP-requests.

import requester from '@sitevision/api/client/requester';

Methods

requester.doGet(options)

Performs a GET-request. options extend base options.

/*
  Base options GET
  {
    type: 'get',
    dataType: 'json'
  }
*/

import requester from '@sitevision/api/client/requester';

const App = () => {
   //...
   const getSomething = (options) => {
      requester
         .doGet({
            url: options.url,
         })
         .then((response) => {
            //...
         })
         .catch((response) => {
            //...
         });
   };
};

export default App;

requester.doPut(options)

Performs a PUT-request. Options extend base options.

/*
  Base options PUT
  {
    type: 'put',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8'
  }
*/

requester
   .doPut({
      url: options.url,
      data: {
         value: options.value,
      },
   })
   .then((response) => {
      //...
   });

requester.doPost(options)

Performs a POST-request. Options extend base options.

/*
  Base options POST
  {
    type: 'post',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8'
  }
*/

requester
   .doPost({
      url: options.url,
      data: {
         value: options.value,
      },
   })
   .then((response) => {
      //...
   });

requester.doPatch(options) [@since 2023.02.1]

Performs a PATCH-request. Options extend base options.

/*
  Base options PATCH
  {
    type: 'patch',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8'
  }
*/

requester
  .doPatch({
    url: options.url,
    data: {
      value: options.value,
    },
  })
  .then((response) => {
    //...
  });

requester.doDelete(options)

Performs a DELETE-request. Options extend base options.

/*
  Base options DELETE
  {
    type: 'delete',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8'
  }
*/

requester
   .doDelete({
      url: options.url,
      data: {
         value: options.value,
      },
   })
   .then((response) => {
      //...
   });

Uploading files with the requester

You can upload files with the requester using post/put requests. In order to do this files should be passed as FormData according to this example and a fileUpload: true property should be added to the request. Setting fileUpload: true is a shortcut for setting contentType: false and processData: false which are properties handled by $.ajax.

const formData = new FormData();
formData.append('username', 'Chris');
formData.append('userpic', myFileInput.files[0], 'chris.jpg');

requester
   .doPost({
      url: options.url,
      data: formData,
      fileUpload: true,
   })
   .then((response) => {
      //...
   });

Want to know more about how you handle files on the serverside see this page.