{"id":207323,"date":"2025-07-08T11:22:26","date_gmt":"2025-07-08T03:22:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207323\/"},"modified":"2025-07-08T11:22:26","modified_gmt":"2025-07-08T03:22:26","slug":"%e5%af%b9aws-lambda%e5%87%bd%e6%95%b0%e7%9a%84curl%e8%af%b7%e6%b1%82%e6%b2%a1%e6%9c%89%e6%94%b6%e5%88%b0json","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207323\/","title":{"rendered":"\u5bf9AWS lambda\u51fd\u6570\u7684curl\u8bf7\u6c42\u6ca1\u6709\u6536\u5230json"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5bf9AWS lambda\u51fd\u6570\u7684curl\u8bf7\u6c42\u6ca1\u6709\u6536\u5230json<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 18:36:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5bf9AWS lambda\u51fd\u6570\u7684curl\u8bf7\u6c42\u6ca1\u6709\u6536\u5230json\u300b\uff0c\u8986\u76d6\u4e86Golang\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u76f4\u63a5\u4ece lambda \u6559\u7a0b\u7684 go \u90e8\u5206\u590d\u5236\u4e86\u4ee3\u7801<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"encoding\/json\"\n    \"fmt\"\n    \"log\"\n\n    \"github.com\/aws\/aws-lambda-go\/lambda\"\n)\n\ntype myevent struct {\n    name string `json:\"name\"`\n    age  int    `json:\"age\"`\n}\n\ntype myresponse struct {\n    message string `json:\"answer\"`\n}\n\nfunc handlelambdaevent(ctx context.context, event myevent) (myresponse, error) {\n    \/\/ event\n    eventjson, _ := json.marshalindent(event, \"\", \"  \")\n    log.printf(\"event: %s\", eventjson)\n\n    return myresponse{message: fmt.sprintf(\"%s is %d years old!\", event.name, event.age)}, nil\n}\n\nfunc main() {\n    lambda.start(handlelambdaevent)\n}<\/pre>\n<p>\u6211\u521b\u5efa\u4e86\u4e00\u4e2a .zip \u6587\u4ef6\u3001\u4e00\u4e2a\u89d2\u8272\u3001\u9644\u52a0\u8be5\u89d2\u8272\u5e76\u5728 lambda \u4e0a\u521b\u5efa\u4e86\u8be5\u51fd\u6570\u3002<\/p>\n<p> \u6211\u53ef\u4ee5\u4f7f\u7528 aws lambda \u6210\u529f\u8c03\u7528\u8be5\u51fd\u6570<\/p>\n<pre># aws lambda invoke --function-name my-function --cli-binary-format raw-in-base64-out --payload '{\"name\": \"kevin\", \"age\":62}' output.txt\n\n# cat output.txt\n{\"answer\":\"kevin is 62 years old!\"}<\/pre>\n<p>\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u51fd\u6570 url\uff0c\u53ef\u4ee5\u901a\u8fc7 awscurl\uff08\u6216curl\uff09\u8c03\u7528\u5b83<\/p>\n<pre># curl -x post https:\/\/xxxxxxxxxx.lambda-url.us-east-1.on.aws\/  \\\n    -h 'content-type: application\/json' \\\n    --user aws_id:aws_key \\\n    --aws-sigv4 \"aws:amz:us-east-1:lambda \\\n    -d '{\"name\": \"kevin\", \"age\": 62}'<\/pre>\n<p>\u4f46\u662f json \u4e0d\u4f1a\u8f6c\u5230\u51fd\u6570\uff08\u4ece\u65e5\u5fd7\u4e2d\u9a8c\u8bc1\uff09\uff0c\u56e0\u6b64\u4e0d\u4f1a\u88ab <strong>my-function<\/strong> \u5904\u7406<\/p>\n<pre>{\"answer\":\" is 0 years old!\"}<\/pre>\n<p>\u6211\u5df2\u7ecf\u5bf9 <code>lambda:invokefunctionurl<\/code> \u8fdb\u884c\u4e86\u6a21\u7cca\u5904\u7406\uff0c\u4f46\u8fd9\u6ca1\u6709\u4efb\u4f55\u533a\u522b<\/p>\n<p>\u66f4\u65b0\uff1a\u6211\u5bf9 <code>handlelambdarequest<\/code> \u51fd\u6570\u8fdb\u884c\u4e86\u76f8\u5f53\u591a\u7684\u66f4\u6539\uff0c\u8bf7\u53c2\u89c1\u4e0b\u6587<\/p>\n<pre>func HandleLambdaEvent(ctx context.Context, request map[string]interface{}) (MyResponse, error) {\n    \/\/ event\n    jsonStr, err := json.Marshal(request)\n    if err != nil {\n        return MyResponse{Message: \"can't unmarshal request\"}, nil\n    }\n    var event Event\n    if err = json.Unmarshal(jsonStr, &amp;event); err != nil {\n        return MyResponse{Message: err.Error()}, nil\n    }\n    if event.Name == \"\" {\n        var apigate events.APIGatewayV2HTTPRequest\n        if err = json.Unmarshal(jsonStr, &amp;apigate); err != nil {\n            return MyResponse{Message: \"can't get event or request\"}, nil\n        }\n        body := []byte(apigate.Body)\n        if err = json.Unmarshal(body, &amp;event); err != nil {\n            return MyResponse{Message: \"can't unmarshal api.Body\"}, nil\n        }\n        return MyResponse{Message: fmt.Sprintf(\"url %s:%d\", event.Name, event.Age)}, nil\n    }\n\n    return MyResponse{Message: fmt.Sprintf(\"invoke %s:%d\", event.Name, event.Age)}, nil\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5f53\u60a8\u901a\u8fc7\u51fd\u6570 url \u8c03\u7528\u51fd\u6570\u65f6\uff0clambda \u4f1a\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u5904\u7406\u60a8\u7684\u8bf7\u6c42\uff01\u8fd9\u4e0d\u662f\u5f88\u76f4\u89c2\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u90a3\u4e9b\u4e0d\u719f\u6089 amazon api gateway \u670d\u52a1\u4e2d\u7684 lambda \u96c6\u6210\u7684\u4eba\u6765\u8bf4\uff0c\u6211\u5e0c\u671b\u4ed6\u4eec\u5728\u6587\u6863\u4e2d\u66f4\u6e05\u695a\u5730\u8bf4\u660e\u8fd9\u4e00\u70b9\u3002\u8fd9\u662f<\/p>\n<pre>type MyEvent struct {\n    ...\n    Body string `json:body`\n}<\/pre>\n<p>\u5e0c\u671b\u8fd9\u6709\u5e2e\u52a9\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5bf9AWS lambda\u51fd\u6570\u7684curl\u8bf7\u6c42\u6ca1\u6709\u6536\u5230json\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207323","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207323"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207323\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}