You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
+8-4Lines changed: 8 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -114,6 +114,8 @@ The above example serves to illustrate some of the process to make a request to
114
114
115
115
Regardless of method, you can execute the fetch requests through Umbraco's [tryExecute](https://apidocs.umbraco.com/v16/ui-api/classes/packages_core_auth.UmbAuthContext.html#tryexecute) function. This function will handle any errors that occur during the request and will automatically refresh the token if it is expired. If the session is expired, the function will also make sure the user logs in again.
The above example requires a host element illustrated by the use of `this`. This is typically a custom element that extends the `UmbLitElement` class.
132
-
{% endhint %}
132
+
You can read more about the `tryExecute` function in this article:
133
+
134
+
{% content-ref url="../try-execute.md" %}
135
+
[try-execute.md](../try-execute.md)
136
+
{% endcontent-ref %}
133
137
134
138
## Conclusion
135
139
136
140
The Fetch API is a powerful and flexible way to make network requests in JavaScript. It is available in all modern browsers and is the recommended way to make network requests in JavaScript. The Fetch API can be used in Umbraco to make network requests to the server. It can also be used to make requests to the Management API controllers. You can use the Fetch API to make requests to any endpoint in the Management API. You can also use it to handle responses in a variety of formats. This is especially useful, if you have but a few requests to make.
137
141
138
-
However, if you have a lot of requests to make, you might want to consider an alternative approach. You could use a library like [@hey-api/openapi-ts](https://heyapi.dev/openapi-ts/get-started) to generate a TypeScript client. The library requires an OpenAPI definition and allows you to make requests to the Management API without having to manually write the requests yourself. The generated client will only need the token once. This can save you a lot of time and effort when working with the Management API. The Umbraco Backoffice itself is running with this library and even exports its internal HTTP client.
142
+
However, if you have a lot of requests to make, you might want to consider an alternative approach. You could use a library like [@hey-api/openapi-ts](https://heyapi.dev/openapi-ts/get-started) to generate a TypeScript client. The library requires an OpenAPI definition and allows you to make requests to the Management API without having to manually write the requests yourself. The generated client will only need the token once. This can save you a lot of time and effort when working with the Management API. The Umbraco Backoffice itself is running with this library and even exports its internal HTTP client. You can read more about this in the [HTTP Client](http-client.md) article.
Copy file name to clipboardExpand all lines: 16/umbraco-cms/customizing/foundation/fetching-data/http-client.md
+7-38Lines changed: 7 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,50 +22,19 @@ The above example shows how to use the HTTP client to make a GET request to the
22
22
23
23
The HTTP client automatically handles authentication and error handling, so you don't have to worry about those details. It also provides a convenient way to parse the response data as JSON.
24
24
25
-
## Executing the request
25
+
## Using the HTTP Client
26
26
27
-
The Backoffice also provides a `tryExecute` function that you can use to execute requests. This function will additionally wrap the request in a try/catch block and handle any errors that occur during the request. It will also show a notification if the request fails.
27
+
The HTTP client is a wrapper around the Fetch API that provides a more convenient way to make network requests. It handles things like request and response parsing, error handling, and retries. The HTTP client is available through the `@umbraco-cms/backoffice/http-client` package, which is included in the Umbraco Backoffice. You can use it to make requests to any endpoint in the Management API or to any other API.
The recommended approach to use the Umbraco HTTP Client is to use the `tryExecute` function. This function will handle any errors that occur during the request and will automatically refresh the token if it is expired. If the session is expired, the function will also make sure the user logs in again.
36
30
37
-
if (error) {
38
-
console.error('There was a problem with the fetch operation:', error);
39
-
} else {
40
-
console.log(data); // Do something with the data
41
-
}
42
-
```
43
-
44
-
The `tryExecute` function takes the context of the current class or element as the first argument and the request as the second argument. Therefore, the above example can be used in any class or element that extends from either the [UmbController](https://apidocs.umbraco.com/v16/ui-api/interfaces/libs_controller-api.UmbController.html) or [UmbLitElement](https://apidocs.umbraco.com/v16/ui-api/classes/packages_core_lit-element.UmbLitElement.html) classes.
31
+
You can read more about the `tryExecute` function in this article:
45
32
46
-
It is recommended to use the `tryExecute` function instead of the raw HTTP client. It can also be configured not to show notifications, if you want to handle errors yourself:
47
-
48
-
```javascript
49
-
tryExecute(this, request, {
50
-
disableNotifications:true,
51
-
});
52
-
```
53
-
54
-
### Cancelling requests
55
-
56
-
The HTTP client also supports cancelling requests. This is useful if you want to cancel a request that is taking too long or if the user navigates away from the page. You can cancel a request by using the [AbortController API](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). The `AbortController` API is a built-in API in modern browsers that allows you to cancel requests. You can use it directly with tryExecute:
33
+
{% content-ref url="../try-execute.md" %}
34
+
[try-execute.md](../try-execute.md)
35
+
{% endcontent-ref %}
57
36
58
37
```javascript
59
-
constabortController=newAbortController();
60
-
61
-
// Cancel the request before starting it for illustration purposes
description:: Learn how to execute requests in the Backoffice.
3
+
---
4
+
5
+
# Executing Requests
6
+
7
+
When you make a request to the server, you need to execute it. This can be done using the Fetch API or the Umbraco HTTP client. The Backoffice also provides a `tryExecute` function that you can use to execute requests. This function will handle any errors that occur during the request and will automatically refresh the token if it is expired. If the session is expired, the function will also make sure the user logs in again.
8
+
9
+
{% hint style="info" %}
10
+
You can read the technical documentation for the `tryExecute` function in the [UI API Documentation](https://apidocs.umbraco.com/v16/ui-api/functions/packages_core_resources.tryExecute.html) class.
11
+
{% endhint %}
12
+
13
+
## Using the HTTP Client
14
+
15
+
Here is an example of how to use the `tryExecute` function with the Umbraco HTTP client:
console.error('There was a problem with the fetch operation:', error);
27
+
} else {
28
+
console.log(data); // Do something with the data
29
+
}
30
+
```
31
+
32
+
The `tryExecute` function takes the context of the current class or element as the first argument and the request as the second argument. Therefore, the above example can be used in any class or element that extends from either the [UmbController](https://apidocs.umbraco.com/v16/ui-api/interfaces/libs_controller-api.UmbController.html) or [UmbLitElement](https://apidocs.umbraco.com/v16/ui-api/classes/packages_core_lit-element.UmbLitElement.html) classes.
33
+
34
+
{% hint style="info" %}
35
+
The above example requires a host element illustrated by the use of `this`. This is typically a custom element that extends the `UmbLitElement` class.
36
+
{% endhint %}
37
+
38
+
It is recommended to always use the `tryExecute` function to wrap HTTP requests.
39
+
40
+
### Disable Notifications
41
+
42
+
The `tryExecute` function will automatically show error bubbles if a request fails. There may be valid cases where you want handle errors yourself. This could for instance be if you want to show a custom error message. You can disable the notifications by passing the `disableNotifications` option to the `tryExecute` function:
43
+
44
+
```javascript
45
+
tryExecute(this, request, {
46
+
disableNotifications:true,
47
+
});
48
+
```
49
+
50
+
### Cancelling requests
51
+
52
+
The `tryExecute` function also supports cancelling requests. This is useful if you want to cancel a request that is taking too long or if the user navigates away from the page. You can cancel a request by using the [AbortController API](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). The `AbortController` API is a built-in API in modern browsers that allows you to cancel requests. You can use it directly with tryExecute:
53
+
54
+
```javascript
55
+
constabortController=newAbortController();
56
+
57
+
// Cancel the request before starting it for illustration purposes
0 commit comments