Skip to content

Commit 8bbe566

Browse files
waldekmastykarzVesaJuvonen
authored andcommitted
Added guidance on connecting to anonymous APIs (SharePoint#2479)
1 parent 7d99b32 commit 8bbe566

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Connect to anonymous APIs
3+
description: Different approaches of connecting to anonymous APIs from your SharePoint Framework solutions
4+
ms.date: 07/06/2018
5+
ms.prod: sharepoint
6+
---
7+
8+
# Connect to anonymous APIs
9+
10+
When building SharePoint Framework solutions, you might want to consume public APIs, such as stock or weather information. This article outlines how to connect to public APIs in SharePoint Framework solutions.
11+
12+
> [!NOTE]
13+
> In this article, public and anonymous APIs are used interchangeably. This article is about connecting to APIs, that don't require authentication at all or are secured with a function/API key that can be passed via query string parameters. See other pages in this section of the documentation for more information about connecting to the [SharePoint APIs](connect-to-sharepoint.md) or [APIs secured with Azure AD](use-aadhttpclient.md).
14+
15+
## Connect to anonymous APIs using the HttpClient
16+
17+
The easiest way, to connect to anonymous APIs in your SharePoint Framework solutions, is by using the HttpClient provided as a part of the SharePoint Framework. For example, to get weather information for London from the public OpenWeatherMap service, you would execute:
18+
19+
```ts
20+
this.context.httpClient
21+
.get('https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22', HttpClient.configurations.v1)
22+
.then((res: HttpClientResponse): Promise<any> => {
23+
return res.json();
24+
})
25+
.then((weather: any): void => {
26+
console.log(weather);
27+
});
28+
```
29+
30+
Similarly to the SPHttpClient you use for connecting to SharePoint APIs, the HttpClient offers you similar capabilities for performing the most common web requests. If necessary, you can use its options, to configure requests. For example, to specify request headers, you would use the following code:
31+
32+
```ts
33+
this.context.httpClient
34+
.get('https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22', HttpClient.configurations.v1,
35+
{
36+
headers: [
37+
['accept', 'text/xml']
38+
]
39+
})
40+
.then((res: HttpClientResponse): Promise<any> => {
41+
return res.json();
42+
})
43+
.then((weather: any): void => {
44+
console.log(weather);
45+
});
46+
```
47+
48+
### Considerations for using the HttpClient
49+
50+
When using the HttpClient, there are a few things that you should take into account.
51+
52+
#### Authentication cookies not included
53+
54+
While the HttpClient is very similar to the SPHttpClient, it doesn't include authentication cookies in its requests. So if you were to use it to connect to SharePoint APIs, your requests would fail with a 401 Unauthorized response.
55+
56+
#### Part of the SharePoint Framework
57+
58+
The HttpClient is part of the SharePoint Framework and you don't need any additional dependencies to start using it. It is already available on the page which is why using it doesn't cause additional performance overhead on runtime.

docs/toc.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@
142142
- name: Connect to APIs secured with Azure AD
143143
href: spfx/web-parts/guidance/connect-to-api-secured-with-aad.md
144144
- name: Call the Microsoft Graph API using OAuth
145-
href: spfx/web-parts/guidance/call-microsoft-graph-from-your-web-part.md
146-
items:
145+
href: spfx/web-parts/guidance/call-microsoft-graph-from-your-web-part.md
146+
- name: Connect to anonymous APIs
147+
href: spfx/connect-to-anonymous-apis.md
147148
- name: Tutorials
148149
items:
149150
- name: Consume Microsoft Graph (tutorial)

0 commit comments

Comments
 (0)