github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/doc/content/en/docs/javascript/http.md (about)

     1  
     2  ---
     3  title: "http"
     4  linkTitle: "http"
     5  date: 2021-10-20
     6  description: >
     7  
     8  ---
     9  
    10  In the **Smart Home** project, there is a capability to perform arbitrary synchronous HTTP requests to external resources.
    11  
    12  The `http` object allows you to make synchronous HTTP requests to external resources, such as API services, and receive responses. You can use this method to integrate with other systems and retrieve or send data through the HTTP protocol in your **Smart Home** project.
    13  
    14  Supported methods include:
    15  * GET
    16  * POST
    17  * PUT
    18  * DELETE
    19  
    20  {{< alert color="success" >}}This function is available in any system script.{{< /alert >}}
    21  
    22  ----------------
    23  
    24  For this purpose, the corresponding methods are available:
    25  
    26  ### GET Request
    27  ```coffeescript
    28  response = http.get(url)
    29  ```
    30  
    31  |  Parameter  | Description  |
    32  |-------------|---------|
    33  | url |    The request URL   |
    34  | response | The response of the request |
    35  
    36  ----------------
    37  
    38  ### POST Request
    39  ```coffeescript
    40  response = http.post(url, body)
    41  ```
    42  
    43  |  Parameter  | Description  |
    44  |-------------|---------|
    45  | url |    The request URL   |
    46  | body |    The request body   |
    47  | response | The response of the request |
    48  
    49  ----------------
    50  
    51  ### Headers Request
    52  ```coffeescript
    53  response = http.headers(headers).post(url, body)
    54  ```
    55  
    56  |  Parameter  | Description  |
    57  |-------------|---------|
    58  | headers |    The request headers   |
    59  | url |    The request URL   |
    60  | body |    The request body   |
    61  | response | The response of the request |
    62  
    63  ----------------
    64  
    65  ### пример кода
    66  
    67  ```coffeescript
    68  # auth
    69  # ##################################
    70  
    71  res = http.digestAuth('user','password').download(uri);
    72  
    73  res = http.basicAuth('user','password').download(uri);
    74  
    75  res = http.download(uri);
    76  
    77  
    78  # GET http
    79  # ##################################
    80  
    81  res = http.get("%s")
    82  if res.error
    83    return
    84  p = JSON.parse(res.body)
    85  
    86  
    87  # POST http
    88  # ##################################
    89  
    90  res = http.post("%s", {'foo':'bar'})
    91  if res.error
    92    return
    93  p = JSON.parse(res.body)
    94  
    95  
    96  # PUT http
    97  # ##################################
    98  
    99  res = http.put("%s", {'foo':'bar'})
   100  if res.error
   101    return
   102  p = JSON.parse(res.body)
   103  
   104  
   105  # GET http + custom headers
   106  # ##################################
   107  
   108  res = http.headers([{'apikey':'some text'}]).get("%s")
   109  if res.error
   110  return
   111  p = JSON.parse(res.body)
   112  
   113  # DELETE http
   114  # ##################################
   115  
   116  res = http.delete("%s")
   117  if res.error
   118    return
   119  p = JSON.parse(res.body)
   120  
   121  ```