@@ -116,6 +116,15 @@ func (r *RequestAccessor) ProxyEventToHTTPRequest(req events.APIGatewayProxyRequ
116
116
return addToHeader (httpRequest , req )
117
117
}
118
118
119
+ func (r * RequestAccessor ) ProxyEventToHTTPRequestV2 (req events.APIGatewayV2HTTPRequest ) (* http.Request , error ) {
120
+ httpRequest , err := r .EventToRequestV2 (req )
121
+ if err != nil {
122
+ log .Println (err )
123
+ return nil , err
124
+ }
125
+ return addToHeaderV2 (httpRequest , req )
126
+ }
127
+
119
128
// EventToRequestWithContext converts an API Gateway proxy event and context into an http.Request object.
120
129
// Returns the populated http request with lambda context, stage variables and APIGatewayProxyRequestContext as part of its context.
121
130
// Access those using GetAPIGatewayContextFromContext, GetStageVarsFromContext and GetRuntimeContextFromContext functions in this package.
@@ -128,6 +137,15 @@ func (r *RequestAccessor) EventToRequestWithContext(ctx context.Context, req eve
128
137
return addToContext (ctx , httpRequest , req ), nil
129
138
}
130
139
140
+ func (r * RequestAccessor ) EventToRequestWithContextV2 (ctx context.Context , req events.APIGatewayV2HTTPRequest ) (* http.Request , error ) {
141
+ httpRequest , err := r .EventToRequestV2 (req )
142
+ if err != nil {
143
+ log .Println (err )
144
+ return nil , err
145
+ }
146
+ return addToContextV2 (ctx , httpRequest , req ), nil
147
+ }
148
+
131
149
// EventToRequest converts an API Gateway proxy event into an http.Request object.
132
150
// Returns the populated request maintaining headers
133
151
func (r * RequestAccessor ) EventToRequest (req events.APIGatewayProxyRequest ) (* http.Request , error ) {
@@ -196,6 +214,73 @@ func (r *RequestAccessor) EventToRequest(req events.APIGatewayProxyRequest) (*ht
196
214
return httpRequest , nil
197
215
}
198
216
217
+ func (r * RequestAccessor ) EventToRequestV2 (req events.APIGatewayV2HTTPRequest ) (* http.Request , error ) {
218
+ decodedBody := []byte (req .Body )
219
+ if req .IsBase64Encoded {
220
+ base64Body , err := base64 .StdEncoding .DecodeString (req .Body )
221
+ if err != nil {
222
+ return nil , err
223
+ }
224
+ decodedBody = base64Body
225
+ }
226
+
227
+ path := req .RequestContext .HTTP .Path
228
+ if r .stripBasePath != "" && len (r .stripBasePath ) > 1 {
229
+ if strings .HasPrefix (path , r .stripBasePath ) {
230
+ path = strings .Replace (path , r .stripBasePath , "" , 1 )
231
+ }
232
+ }
233
+ if ! strings .HasPrefix (path , "/" ) {
234
+ path = "/" + path
235
+ }
236
+ serverAddress := DefaultServerAddress
237
+ if customAddress , ok := os .LookupEnv (CustomHostVariable ); ok {
238
+ serverAddress = customAddress
239
+ }
240
+ path = serverAddress + path
241
+
242
+ if len (req .QueryStringParameters ) > 0 {
243
+ queryString := ""
244
+ for q , l := range req .QueryStringParameters {
245
+ values := strings .Split (l , "," )
246
+ for _ , v := range values {
247
+ if queryString != "" {
248
+ queryString += "&"
249
+ }
250
+ queryString += url .QueryEscape (q ) + "=" + url .QueryEscape (v )
251
+ }
252
+ }
253
+ path += "?" + queryString
254
+ } else if len (req .QueryStringParameters ) > 0 {
255
+ // Support `QueryStringParameters` for backward compatibility.
256
+ // https://github.com/awslabs/aws-lambda-go-api-proxy/issues/37
257
+ queryString := ""
258
+ for q := range req .QueryStringParameters {
259
+ if queryString != "" {
260
+ queryString += "&"
261
+ }
262
+ queryString += url .QueryEscape (q ) + "=" + url .QueryEscape (req .QueryStringParameters [q ])
263
+ }
264
+ path += "?" + queryString
265
+ }
266
+
267
+ httpRequest , err := http .NewRequest (
268
+ strings .ToUpper (req .RequestContext .HTTP .Method ),
269
+ path ,
270
+ bytes .NewReader (decodedBody ),
271
+ )
272
+
273
+ if err != nil {
274
+ fmt .Printf ("Could not convert request %s:%s to http.Request\n " , req .RequestContext .HTTP .Method , req .RequestContext .HTTP .Path )
275
+ log .Println (err )
276
+ return nil , err
277
+ }
278
+ for h := range req .Headers {
279
+ httpRequest .Header .Add (h , req .Headers [h ])
280
+ }
281
+ return httpRequest , nil
282
+ }
283
+
199
284
func addToHeader (req * http.Request , apiGwRequest events.APIGatewayProxyRequest ) (* http.Request , error ) {
200
285
stageVars , err := json .Marshal (apiGwRequest .StageVariables )
201
286
if err != nil {
@@ -212,13 +297,36 @@ func addToHeader(req *http.Request, apiGwRequest events.APIGatewayProxyRequest)
212
297
return req , nil
213
298
}
214
299
300
+ func addToHeaderV2 (req * http.Request , apiGwRequest events.APIGatewayV2HTTPRequest ) (* http.Request , error ) {
301
+ stageVars , err := json .Marshal (apiGwRequest .StageVariables )
302
+ if err != nil {
303
+ log .Println ("Could not marshal stage variables for custom header" )
304
+ return nil , err
305
+ }
306
+ req .Header .Add (APIGwStageVarsHeader , string (stageVars ))
307
+ apiGwContext , err := json .Marshal (apiGwRequest .RequestContext )
308
+ if err != nil {
309
+ log .Println ("Could not Marshal API GW context for custom header" )
310
+ return req , err
311
+ }
312
+ req .Header .Add (APIGwContextHeader , string (apiGwContext ))
313
+ return req , nil
314
+ }
315
+
215
316
func addToContext (ctx context.Context , req * http.Request , apiGwRequest events.APIGatewayProxyRequest ) * http.Request {
216
317
lc , _ := lambdacontext .FromContext (ctx )
217
318
rc := requestContext {lambdaContext : lc , gatewayProxyContext : apiGwRequest .RequestContext , stageVars : apiGwRequest .StageVariables }
218
319
ctx = context .WithValue (ctx , ctxKey {}, rc )
219
320
return req .WithContext (ctx )
220
321
}
221
322
323
+ func addToContextV2 (ctx context.Context , req * http.Request , apiGwRequest events.APIGatewayV2HTTPRequest ) * http.Request {
324
+ lc , _ := lambdacontext .FromContext (ctx )
325
+ rc := requestContextV2 {lambdaContext : lc , gatewayProxyContext : apiGwRequest .RequestContext , stageVars : apiGwRequest .StageVariables }
326
+ ctx = context .WithValue (ctx , ctxKey {}, rc )
327
+ return req .WithContext (ctx )
328
+ }
329
+
222
330
// GetAPIGatewayContextFromContext retrieve APIGatewayProxyRequestContext from context.Context
223
331
func GetAPIGatewayContextFromContext (ctx context.Context ) (events.APIGatewayProxyRequestContext , bool ) {
224
332
v , ok := ctx .Value (ctxKey {}).(requestContext )
@@ -244,3 +352,9 @@ type requestContext struct {
244
352
gatewayProxyContext events.APIGatewayProxyRequestContext
245
353
stageVars map [string ]string
246
354
}
355
+
356
+ type requestContextV2 struct {
357
+ lambdaContext * lambdacontext.LambdaContext
358
+ gatewayProxyContext events.APIGatewayV2HTTPRequestContext
359
+ stageVars map [string ]string
360
+ }
0 commit comments