@@ -3,7 +3,7 @@ title: Using API Management service to generate HTTP requests
3
3
description : Learn to use request and response policies in API Management to call external services from your API
4
4
services : api-management
5
5
documentationcenter : ' '
6
- author : dlepow
6
+ author : adrianhall
7
7
manager : erikre
8
8
editor : ' '
9
9
@@ -12,8 +12,8 @@ ms.service: api-management
12
12
ms.topic : article
13
13
ms.tgt_pltfrm : na
14
14
ms.workload : na
15
- ms.date : 12/15/2016
16
- ms.author : danlep
15
+ ms.date : 04/14/2022
16
+ ms.author : adhal
17
17
18
18
---
19
19
# 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
29
29
30
30
``` xml
31
31
<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 >
52
52
</choose >
53
53
```
54
54
@@ -144,17 +144,17 @@ At the end, you get the following policy:
144
144
</send-request >
145
145
146
146
<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 >
158
158
<base />
159
159
</inbound >
160
160
```
@@ -198,12 +198,12 @@ Once you have this information, you can make requests to all the backend systems
198
198
</send-request >
199
199
200
200
<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 >
202
202
<set-method >GET</set-method >
203
203
</send-request >
204
204
205
205
<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 >
207
207
<set-method >GET</set-method >
208
208
</send-request >
209
209
```
@@ -235,49 +235,49 @@ The complete policy looks as follows:
235
235
``` xml
236
236
<policies >
237
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())" >
238
+ <set-variable name =" fromDate" value =" @(context.Request.Url.Query[" fromDate" ].Last())" >
239
+ <set-variable name =" toDate" value =" @(context.Request.Url.Query[" toDate" ].Last())" >
240
240
241
- <send-request mode =" new" response-variable-name =" revenuedata" timeout =" 20" ignore-error =" true" >
242
- <set-url >@($"https://accounting.acme.com/salesdata?from={(string)context.Variables["fromDate"]}& to={(string)context.Variables["fromDate"]}")"</set-url >
243
- <set-method >GET</set-method >
244
- </send-request >
241
+ <send-request mode =" new" response-variable-name =" revenuedata" timeout =" 20" ignore-error =" true" >
242
+ <set-url >@($"https://accounting.acme.com/salesdata?from={(string)context.Variables["fromDate"]}& to={(string)context.Variables["fromDate"]}")"</set-url >
243
+ <set-method >GET</set-method >
244
+ </send-request >
245
245
246
- <send-request mode =" new" response-variable-name =" materialdata" timeout =" 20" ignore-error =" true" >
247
- <set-url >@($"https://inventory.acme.com/materiallevels?from={(string)context.Variables["fromDate"]}& to={(string)context.Variables["fromDate"]}")"</set-url >
248
- <set-method >GET</set-method >
249
- </send-request >
246
+ <send-request mode =" new" response-variable-name =" materialdata" timeout =" 20" ignore-error =" true" >
247
+ <set-url >@($"https://inventory.acme.com/materiallevels?from={(string)context.Variables["fromDate"]}& to={(string)context.Variables["fromDate"]}")"</set-url >
248
+ <set-method >GET</set-method >
249
+ </send-request >
250
250
251
- <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 >
253
- <set-method >GET</set-method >
254
- </send-request >
251
+ <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 >
253
+ <set-method >GET</set-method >
254
+ </send-request >
255
255
256
- <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 >
258
- <set-method >GET</set-method >
259
- </send-request >
256
+ <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 >
258
+ <set-method >GET</set-method >
259
+ </send-request >
260
260
261
- <return-response response-variable-name =" existing response variable" >
262
- <set-status code =" 200" reason =" OK" />
263
- <set-header name =" Content-Type" exists-action =" override" >
264
- <value >application/json</value >
265
- </set-header >
266
- <set-body >
267
- @(new JObject(new JProperty("revenuedata",((IResponse)context.Variables["revenuedata"]).Body.As<JObject >()),
268
- new JProperty("materialdata",((IResponse)context.Variables["materialdata"]).Body.As<JObject >()),
269
- new JProperty("throughputdata",((IResponse)context.Variables["throughputdata"]).Body.As<JObject >()),
270
- new JProperty("accidentdata",((IResponse)context.Variables["accidentdata"]).Body.As<JObject >())
271
- ).ToString())
272
- </set-body >
273
- </return-response >
274
- </inbound >
275
- <backend >
276
- <base />
277
- </backend >
278
- <outbound >
279
- <base />
280
- </outbound >
261
+ <return-response response-variable-name =" existing response variable" >
262
+ <set-status code =" 200" reason =" OK" />
263
+ <set-header name =" Content-Type" exists-action =" override" >
264
+ <value >application/json</value >
265
+ </set-header >
266
+ <set-body >
267
+ @(new JObject(new JProperty("revenuedata",((IResponse)context.Variables["revenuedata"]).Body.As<JObject >()),
268
+ new JProperty("materialdata",((IResponse)context.Variables["materialdata"]).Body.As<JObject >()),
269
+ new JProperty("throughputdata",((IResponse)context.Variables["throughputdata"]).Body.As<JObject >()),
270
+ new JProperty("accidentdata",((IResponse)context.Variables["accidentdata"]).Body.As<JObject >())
271
+ ).ToString())
272
+ </set-body >
273
+ </return-response >
274
+ </inbound >
275
+ <backend >
276
+ <base />
277
+ </backend >
278
+ <outbound >
279
+ <base />
280
+ </outbound >
281
281
</policies >
282
282
```
283
283
0 commit comments