@@ -153,35 +153,27 @@ The following sample C# code for an HTTP trigger simulates Event Grid trigger be
153
153
154
154
``` csharp
155
155
[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 )
157
157
{
158
158
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
+ }
159
168
160
169
var requestmessage = await req .Content .ReadAsStringAsync ();
161
170
var message = JToken .Parse (requestmessage );
162
171
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 ()}" );
185
177
186
178
return req .CreateResponse (HttpStatusCode .OK );
187
179
}
@@ -192,22 +184,26 @@ The following sample JavaScript code for an HTTP trigger simulates Event Grid tr
192
184
``` javascript
193
185
module .exports = function (context , req ) {
194
186
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
+
199
191
context.log('Validate request received');
200
- var code = message[0 ].data .validationCode ;
201
192
context.res = { status: 200, body: { " ValidationResponse" : code } };
193
+ context.res.headers.append('Webhook-Allowed-Origin', 'eventgrid.azure.net');
202
194
}
203
- else {
195
+ else
196
+ {
197
+ var message = req.body;
198
+
204
199
// The request is not for subscription validation, so it's for an event.
205
200
// CloudEvents schema delivers one event at a time.
206
201
var event = JSON.parse(message);
207
202
context.log('Source: ' + event.source);
208
203
context.log('Time: ' + event.eventTime);
209
204
context.log('Data: ' + JSON.stringify(event.data));
210
205
}
206
+
211
207
context.done();
212
208
};
213
209
```
0 commit comments