Skip to content

Commit d7ef53f

Browse files
authored
Merge pull request #51064 from grandhiramesh/patch-1
Update examples for subscription validation in EventGrid subscription
2 parents e09195f + e188734 commit d7ef53f

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

articles/event-grid/cloudevents-schema.md

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -153,35 +153,27 @@ The following sample C# code for an HTTP trigger simulates Event Grid trigger be
153153

154154
```csharp
155155
[FunctionName("HttpTrigger")]
156-
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
156+
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "options", Route = null)]HttpRequestMessage req, ILogger log)
157157
{
158158
log.LogInformation("C# HTTP trigger function processed a request.");
159+
if (req.Method == "OPTIONS")
160+
{
161+
// If the request is for subscription validation, send back the validation code
162+
163+
var response = req.CreateResponse(HttpStatusCode.OK);
164+
response.Add("Webhook-Allowed-Origin", "eventgrid.azure.net");
165+
166+
return response;
167+
}
159168

160169
var requestmessage = await req.Content.ReadAsStringAsync();
161170
var message = JToken.Parse(requestmessage);
162171

163-
if (message.Type == JTokenType.Array)
164-
{
165-
// If the request is for subscription validation, send back the validation code.
166-
if (string.Equals((string)message[0]["eventType"],
167-
"Microsoft.EventGrid.SubscriptionValidationEvent",
168-
System.StringComparison.OrdinalIgnoreCase))
169-
{
170-
log.LogInformation("Validate request received");
171-
return req.CreateResponse<object>(new
172-
{
173-
validationResponse = message[0]["data"]["validationCode"]
174-
});
175-
}
176-
}
177-
else
178-
{
179-
// The request is not for subscription validation, so it's for an event.
180-
// CloudEvents schema delivers one event at a time.
181-
log.LogInformation($"Source: {message["source"]}");
182-
log.LogInformation($"Time: {message["eventTime"]}");
183-
log.LogInformation($"Event data: {message["data"].ToString()}");
184-
}
172+
// The request is not for subscription validation, so it's for an event.
173+
// CloudEvents schema delivers one event at a time.
174+
log.LogInformation($"Source: {message["source"]}");
175+
log.LogInformation($"Time: {message["eventTime"]}");
176+
log.LogInformation($"Event data: {message["data"].ToString()}");
185177

186178
return req.CreateResponse(HttpStatusCode.OK);
187179
}
@@ -192,22 +184,26 @@ The following sample JavaScript code for an HTTP trigger simulates Event Grid tr
192184
```javascript
193185
module.exports = function (context, req) {
194186
context.log('JavaScript HTTP trigger function processed a request.');
195-
196-
var message = req.body;
197-
// If the request is for subscription validation, send back the validation code.
198-
if (message.length > 0 && message[0].eventType == "Microsoft.EventGrid.SubscriptionValidationEvent") {
187+
188+
if (req.method == "OPTIONS) {
189+
// If the request is for subscription validation, send back the validation code
190+
199191
context.log('Validate request received');
200-
var code = message[0].data.validationCode;
201192
context.res = { status: 200, body: { "ValidationResponse": code } };
193+
context.res.headers.append('Webhook-Allowed-Origin', 'eventgrid.azure.net');
202194
}
203-
else {
195+
else
196+
{
197+
var message = req.body;
198+
204199
// The request is not for subscription validation, so it's for an event.
205200
// CloudEvents schema delivers one event at a time.
206201
var event = JSON.parse(message);
207202
context.log('Source: ' + event.source);
208203
context.log('Time: ' + event.eventTime);
209204
context.log('Data: ' + JSON.stringify(event.data));
210205
}
206+
211207
context.done();
212208
};
213209
```

0 commit comments

Comments
 (0)