douqiao7958 2019-07-18 04:54
浏览 37

API-网关集成请求HTTP标头未将查询字符串映射到标头

On Api-Gateway i'm trying so set up mapping from 'Method Request' query string to 'integration request' headers to lambda but the mapping never reach the lambda function.

On 'Method Request' > 'URL Query String Parameters' I set it up the name 'customerIdentification'

Then as the documentation says: doc

went to 'Integration Request' > 'HTTP Headers' add a name 'userId' and mapped to 'method.request.querystring.customerIdentification'

package main

import (
    "context"
    "encoding/json"
    "fmt"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {

    fmt.Printf("Processing request data for request %s.
", request.RequestContext.RequestID)
    fmt.Printf("Body size = %d.
", len(request.Body))

    fmt.Println("Headers:")
    for key, value := range request.Headers {
        fmt.Printf("    %s: %s
", key, value)
    }
    xxx, err := json.Marshal(request.Headers)
    if err != nil {
        fmt.Println("*** err *** err *** err *** err *** err ")
        fmt.Println(err)
        fmt.Println("*** err *** err *** err *** err *** err ")
    }
    return events.APIGatewayProxyResponse{Body: string(xxx), StatusCode: 200}, nil
}

func main() {
    lambda.Start(handleRequest)
}

I expect that on the golang lambda function code I can retrieve the 'userId' from 'request.Headers'.

but it always come empty

  • 写回答

2条回答 默认 最新

  • duancuisan2503 2019-07-18 05:27
    关注

    To retrieve a value for any key from map in golang you can simply do like this

    val, ok := request.Headers["userId"]
    if ok { // the key is present
        fmt.Println(val)
    }
    

    But are you sure, the key 'userId' is in header? usually these kind of keys will be in body only. If you want to cross check try to unmarshal your request.Body into map[string]string and retrieve "userd" from there.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀