Skip to content

Commit 9ea0835

Browse files
authored
[Preview 5] Update call-web-api.md (#17933)
1 parent 682fc99 commit 9ea0835

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

aspnetcore/blazor/call-web-api.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,26 +185,26 @@ When running on WebAssembly in a Blazor WebAssembly app, use [HttpClient](xref:f
185185
```razor
186186
@using System.Net.Http
187187
@using System.Net.Http.Headers
188+
@using System.Net.Http.Json
188189
@inject HttpClient Http
189190
190191
@code {
191192
private async Task PostRequest()
192193
{
193-
Http.DefaultRequestHeaders.Authorization =
194-
new AuthenticationHeaderValue("Bearer", "{OAUTH TOKEN}");
195-
196194
var requestMessage = new HttpRequestMessage()
197195
{
198196
Method = new HttpMethod("POST"),
199197
RequestUri = new Uri("https://localhost:10000/api/TodoItems"),
200198
Content =
201-
new StringContent(
202-
@"{""name"":""A New Todo Item"",""isComplete"":false}")
199+
JsonContent.Create(new TodoItem
200+
{
201+
Name: "A New Todo Item",
202+
IsComplete: false
203+
})
203204
};
204-
205-
requestMessage.Content.Headers.ContentType =
206-
new System.Net.Http.Headers.MediaTypeHeaderValue(
207-
"application/json");
205+
206+
requestMessage.Headers.Authorization =
207+
new AuthenticationHeaderValue("Bearer", "{OAUTH TOKEN}");
208208
209209
requestMessage.Content.Headers.TryAddWithoutValidation(
210210
"x-custom-header", "value");
@@ -216,6 +216,8 @@ When running on WebAssembly in a Blazor WebAssembly app, use [HttpClient](xref:f
216216
}
217217
```
218218

219+
.NET WebAssembly's implementation of `HttpClient` uses [WindowOrWorkerGlobalScope.fetch()](https://developer.mozilla.org/docs/Web/API/WindowOrWorkerGlobalScope/fetch). Fetch allows configuring several [request-specific options](https://developer.mozilla.org/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters).
220+
219221
HTTP fetch request options can be configured with `HttpRequestMessage` extension methods shown in the following table.
220222

221223
| `HttpRequestMessage` extension method | Fetch request property |
@@ -229,6 +231,12 @@ You can set additional options using the more generic `SetBrowserRequestOption`
229231

230232
The HTTP response is typically buffered in a Blazor WebAssembly app to enable support for sync reads on the response content. To enable support for response streaming, use the `SetBrowserResponseStreamingEnabled` extension method on the request.
231233

234+
To include credentials in a cross-origin request, use the `SetBrowserRequestCredentials` extension method:
235+
236+
```csharp
237+
requestMessage.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);
238+
```
239+
232240
For more information on Fetch API options, see [MDN web docs: WindowOrWorkerGlobalScope.fetch():Parameters](https://developer.mozilla.org/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters).
233241

234242
When sending credentials (authorization cookies/headers) on CORS requests, the `Authorization` header must be allowed by the CORS policy.

0 commit comments

Comments
 (0)