How to create a JSON web service on drupal 8 ? | Drupal 8

How to create a JSON web service on drupal 8 ?

Submitted by editor on Tue, 07/25/2017 - 10:33
Question

How to create a custom JSON web service with Drupal 8 ?

This is a simple example of a web service. For the example, We will make a service to multiply two numbers.

Create JSON WebService module

Create a module.

Here : mywebservice

Create route, the Web Service path.

Example:
mywebservice.multiply:
  path: '/mywebservice/maths/multiply'
  defaults:
    _controller: '\Drupal\mywebservice\Controller\MyWebService::multiply'
    _title: 'MyWebService'
  requirements:
    _permission: 'access content'

Create webservice Controller

Example:
<?php
//
namespace Drupal\mywebservice\Controller;
//
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
 * Class MyWebService.
 */
class MyWebService extends ControllerBase {
  /**
   * Multiply.
   */
  public function multiply() {
    $request = \Drupal::request();
    $output['a'] = $request->get('a');
    $output['b'] = $request->get('b');
    $output['result'] = $output['a'] * $output['b'];
    return new JsonResponse($output);
  }
}

Test the web service

Visit : http://yourdomain.loc/mywebservice/maths/multiply
Must return a JSON : {"a":null,"b":null,"result":0}

Visit : http://yourdomain.loc/mywebservice/maths/multiply?a=5&b=2
Must return a JSON : {"a":"5","b":"2","result":10}

 

Comments

Will Peavy (not verified)

Fri, 04/27/2018 - 06:15

Thank you. This was exactly what I needed to start building a custom API module.

R HILDAN M (not verified)

Fri, 12/14/2018 - 17:08

ini ke 15 pusat plat astm516 70 pusat plat boiler pusat plat hardox pusat plat hb 400 pusat plat high strenght pusat plat high tensile sm490 yb pusat plat high tensile sm490ya pusat plat kapal pusat plat stainless ss304 pusat plat stainless ss310 Agen Plat Besi Baja Jual besi Pipa sch 80 baja Jual besi beton cakra steel cs Supplier Bondek Cor Supplier Bondek Cor Distributor Plat besi Jual bondek undangan pernikahan artis Jual Besi Pipa Baja Schedule 80 Toko Plat Besi Baja Supplier besi Pipa sch 80 baja Jual besi beton bjku Toko Bondek Cor Toko Bondek Cor Distributor besi h beam Jual wiremesh undangan pernikahan anti mainstream Jual Besi Pipa Baja Schedule 40 Jual Plat Besi Baja Toko besi Pipa sch 80 baja Jual besi beton Sni Ulir Polos Agen Bondek Cor Agen Bondek Cor Distributor besi wf Jual pipa besi baja sch 40 sch 80 undangan pernikahan amplop Jual Pipa Besi Hitam Medium Harga Plat Besi Baja Agen besi Pipa sch 80 baja Jual besi beton ais Pabrik Bondek Cor Pabrik Bondek Cor Distributor wiremesh Jual besi hollow undangan pernikahan mewah Jual Besi Wf Pabrik Plat Besi Baja Pabrik besi Pipa sch 80 baja Jual besi beton Distributor Welded Beam Distributor Welded Beam Distributor besi unp Jual besi beton Sni Ulir Polos undangan pernikahan anime Jual Besi H beam Distributor Besi H beam Baja Distributor Plat Bordes Harga Welded Beam Harga Welded Beam Distributor besi cnp Jual besi wf baja undangan pernikahan elegan Jual Besi Beton Supplier Besi H beam Baja Harga Plat Bordes www.pusatbesibaja.co.id Jual Welded Beam Jual Welded Beam Distributor besi hollow Jual besi unp undangan pernikahan contoh www.sentrabesibaja.com Agen Besi H beam Baja Jual Plat Bordes www.pusatbesibaja.com Supplier Welded Beam Supplier Welded Beam Distributor besi beton Sni Ulir Polos

Add new comment