Skip to content

Commit e8e0bd8

Browse files
authored
Merge pull request #195107 from adrianhall/apim-issue-70584
(azure-docs#70584) Correcting language from GitHub issue
2 parents b545ca8 + 2931f79 commit e8e0bd8

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

articles/api-management/api-management-sample-send-request.md

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Using API Management service to generate HTTP requests
33
description: Learn to use request and response policies in API Management to call external services from your API
44
services: api-management
55
documentationcenter: ''
6-
author: dlepow
6+
author: adrianhall
77
manager: erikre
88
editor: ''
99

@@ -12,8 +12,8 @@ ms.service: api-management
1212
ms.topic: article
1313
ms.tgt_pltfrm: na
1414
ms.workload: na
15-
ms.date: 12/15/2016
16-
ms.author: danlep
15+
ms.date: 04/14/2022
16+
ms.author: adhal
1717

1818
---
1919
# Using external services from the Azure API Management service
@@ -29,26 +29,26 @@ The following example demonstrates how to send a message to a Slack chat room if
2929

3030
```xml
3131
<choose>
32-
<when condition="@(context.Response.StatusCode >= 500)">
33-
<send-one-way-request mode="new">
34-
<set-url>https://hooks.slack.com/services/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</set-url>
35-
<set-method>POST</set-method>
36-
<set-body>@{
37-
return new JObject(
38-
new JProperty("username","APIM Alert"),
39-
new JProperty("icon_emoji", ":ghost:"),
40-
new JProperty("text", String.Format("{0} {1}\nHost: {2}\n{3} {4}\n User: {5}",
41-
context.Request.Method,
42-
context.Request.Url.Path + context.Request.Url.QueryString,
43-
context.Request.Url.Host,
44-
context.Response.StatusCode,
45-
context.Response.StatusReason,
46-
context.User.Email
47-
))
48-
).ToString();
49-
}</set-body>
50-
</send-one-way-request>
51-
</when>
32+
<when condition="@(context.Response.StatusCode >= 500)">
33+
<send-one-way-request mode="new">
34+
<set-url>https://hooks.slack.com/services/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</set-url>
35+
<set-method>POST</set-method>
36+
<set-body>@{
37+
return new JObject(
38+
new JProperty("username","APIM Alert"),
39+
new JProperty("icon_emoji", ":ghost:"),
40+
new JProperty("text", String.Format("{0} {1}\nHost: {2}\n{3} {4}\n User: {5}",
41+
context.Request.Method,
42+
context.Request.Url.Path + context.Request.Url.QueryString,
43+
context.Request.Url.Host,
44+
context.Response.StatusCode,
45+
context.Response.StatusReason,
46+
context.User.Email
47+
))
48+
).ToString();
49+
}</set-body>
50+
</send-one-way-request>
51+
</when>
5252
</choose>
5353
```
5454

@@ -144,17 +144,17 @@ At the end, you get the following policy:
144144
</send-request>
145145

146146
<choose>
147-
<!-- Check active property in response -->
148-
<when condition="@((bool)((IResponse)context.Variables["tokenstate"]).Body.As<JObject>()["active"] == false)">
149-
<!-- Return 401 Unauthorized with http-problem payload -->
150-
<return-response response-variable-name="existing response variable">
151-
<set-status code="401" reason="Unauthorized" />
152-
<set-header name="WWW-Authenticate" exists-action="override">
153-
<value>Bearer error="invalid_token"</value>
154-
</set-header>
155-
</return-response>
156-
</when>
157-
</choose>
147+
<!-- Check active property in response -->
148+
<when condition="@((bool)((IResponse)context.Variables["tokenstate"]).Body.As<JObject>()["active"] == false)">
149+
<!-- Return 401 Unauthorized with http-problem payload -->
150+
<return-response response-variable-name="existing response variable">
151+
<set-status code="401" reason="Unauthorized" />
152+
<set-header name="WWW-Authenticate" exists-action="override">
153+
<value>Bearer error="invalid_token"</value>
154+
</set-header>
155+
</return-response>
156+
</when>
157+
</choose>
158158
<base />
159159
</inbound>
160160
```
@@ -198,19 +198,20 @@ Once you have this information, you can make requests to all the backend systems
198198
</send-request>
199199

200200
<send-request mode="new" response-variable-name="throughputdata" timeout="20" ignore-error="true">
201-
<set-url>@($"https://production.acme.com/throughput?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")</set-url>
201+
<set-url>@($"https://production.acme.com/throughput?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")</set-url>
202202
<set-method>GET</set-method>
203203
</send-request>
204204

205205
<send-request mode="new" response-variable-name="accidentdata" timeout="20" ignore-error="true">
206-
<set-url>@($"https://production.acme.com/accidentdata?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")</set-url>
206+
<set-url>@($"https://production.acme.com/accidentdata?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")</set-url>
207207
<set-method>GET</set-method>
208208
</send-request>
209209
```
210210

211-
These requests execute in sequence, which is not ideal.
211+
API Management will send these requests sequentially.
212212

213213
### Responding
214+
214215
To construct the composite response, you can use the [return-response](./api-management-advanced-policies.md#ReturnResponse) policy. The `set-body` element can use an expression to construct a new `JObject` with all the component representations embedded as properties.
215216

216217
```xml
@@ -233,10 +234,9 @@ The complete policy looks as follows:
233234

234235
```xml
235236
<policies>
236-
<inbound>
237-
238-
<set-variable name="fromDate" value="@(context.Request.Url.Query["fromDate"].Last())">
239-
<set-variable name="toDate" value="@(context.Request.Url.Query["toDate"].Last())">
237+
<inbound>
238+
<set-variable name="fromDate" value="@(context.Request.Url.Query["fromDate"].Last())">
239+
<set-variable name="toDate" value="@(context.Request.Url.Query["toDate"].Last())">
240240

241241
<send-request mode="new" response-variable-name="revenuedata" timeout="20" ignore-error="true">
242242
<set-url>@($"https://accounting.acme.com/salesdata?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")"</set-url>
@@ -249,12 +249,12 @@ The complete policy looks as follows:
249249
</send-request>
250250

251251
<send-request mode="new" response-variable-name="throughputdata" timeout="20" ignore-error="true">
252-
<set-url>@($"https://production.acme.com/throughput?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")"</set-url>
252+
<set-url>@($"https://production.acme.com/throughput?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")"</set-url>
253253
<set-method>GET</set-method>
254254
</send-request>
255255

256256
<send-request mode="new" response-variable-name="accidentdata" timeout="20" ignore-error="true">
257-
<set-url>@($"https://production.acme.com/accidentdata?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")"</set-url>
257+
<set-url>@($"https://production.acme.com/accidentdata?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")"</set-url>
258258
<set-method>GET</set-method>
259259
</send-request>
260260

@@ -268,20 +268,18 @@ The complete policy looks as follows:
268268
new JProperty("materialdata",((IResponse)context.Variables["materialdata"]).Body.As<JObject>()),
269269
new JProperty("throughputdata",((IResponse)context.Variables["throughputdata"]).Body.As<JObject>()),
270270
new JProperty("accidentdata",((IResponse)context.Variables["accidentdata"]).Body.As<JObject>())
271-
).ToString())
271+
).ToString())
272272
</set-body>
273273
</return-response>
274-
</inbound>
275-
<backend>
276-
<base />
277-
</backend>
278-
<outbound>
279-
<base />
280-
</outbound>
274+
</inbound>
275+
<backend>
276+
<base />
277+
</backend>
278+
<outbound>
279+
<base />
280+
</outbound>
281281
</policies>
282282
```
283283

284-
In the configuration of the placeholder operation, you can configure the dashboard resource to be cached for at least an hour.
285-
286284
## Summary
287285
Azure API Management service provides flexible policies that can be selectively applied to HTTP traffic and enables composition of backend services. Whether you want to enhance your API gateway with alerting functions, verification, validation capabilities or create new composite resources based on multiple backend services, the `send-request` and related policies open a world of possibilities.

0 commit comments

Comments
 (0)