postman 无法查询CloudWatch日志AWS API端点

rta7y2nd  于 2023-06-22  发布在  Postman
关注(0)|答案(3)|浏览(134)

我正在尝试为我们的内部团队构建一个小型Web应用程序,用于查看我们的CloudWatch日志。现在,我还处于开发初期,只是尝试使用AWS API官方文档中指定的https://logs.us-east-1.amazonaws.com通过Postman访问日志。
我已经按照以下步骤设置了对端点的POST请求,并使用以下头:Postman Generated Headers
此外,在以下文件我已提供的行动在本职位请求的正文:{"Action": "DescribeLogGroups"}
使用AWS CLI,这工作正常,我可以看到我所有的日志组。
当我向https://logs.us-east-1.amazonaws.com发送这个请求时,我得到了:

{
    "Output": {
        "__type": "com.amazon.coral.service#UnknownOperationException",
        "message": null
    },
    "Version": "1.0"
}

状态码是200。
我尝试过的事情:

  • 完全删除请求体->导致“内部服务器错误”
  • /describeloggroups附加到没有正文的URL->导致“内部服务器错误”

我真的不知道我做错了什么。

db2dz4w8

db2dz4w81#

最好的方法是将X-Amz-Target头设置为Logs_20140328.DescribeLogGroups
下面是一个示例请求:https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_DescribeLogGroups.html#API_DescribeLogGroups_Example_1_Request
下面是一个你可以尝试的 Postman 集合。将其保存为文件并使用File -> Import导入Postman。它还要求您在postman中设置credential和region变量。

{
    "info": {
        "name": "CloudWatch Logs",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "DescribeLogs",
            "request": {
                "auth": {
                    "type": "awsv4",
                    "awsv4": [
                        {
                            "key": "sessionToken",
                            "value": "{{SESSION_TOKEN}}",
                            "type": "string"
                        },
                        {
                            "key": "service",
                            "value": "logs",
                            "type": "string"
                        },
                        {
                            "key": "region",
                            "value": "{{REGION}}",
                            "type": "string"
                        },
                        {
                            "key": "secretKey",
                            "value": "{{SECRET_ACCESS_KEY}}",
                            "type": "string"
                        },
                        {
                            "key": "accessKey",
                            "value": "{{ACCESS_KEY_ID}}",
                            "type": "string"
                        }
                    ]
                },
                "method": "POST",
                "header": [
                    {
                        "warning": "This is a duplicate header and will be overridden by the Content-Type header generated by Postman.",
                        "key": "Content-Type",
                        "type": "text",
                        "value": "application/json"
                    },
                    {
                        "key": "X-Amz-Target",
                        "type": "text",
                        "value": "Logs_20140328.DescribeLogGroups"
                    },
                    {
                        "warning": "This is a duplicate header and will be overridden by the host header generated by Postman.",
                        "key": "host",
                        "type": "text",
                        "value": "logs.{{REGION}}.amazonaws.com"
                    },
                    {
                        "key": "Accept",
                        "type": "text",
                        "value": "application/json"
                    },
                    {
                        "key": "Content-Encoding",
                        "type": "text",
                        "value": "amz-1.0"
                    }
                ],
                "body": {
                    "mode": "raw",
                    "raw": "{}"
                },
                "url": {
                    "raw": "https://logs.{{REGION}}.amazonaws.com",
                    "protocol": "https",
                    "host": [
                        "logs",
                        "{{REGION}}",
                        "amazonaws",
                        "com"
                    ]
                }
            },
            "response": []
        }
    ],
    "protocolProfileBehavior": {}
}
mspsb9vt

mspsb9vt2#

尝试将其复制到json文件中,并将其导入Postman并添加缺少的变量。我试图在服务“日志”中获取DescribeLogGroups。在这里的文档www.example.com中查看https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_DescribeLogGroups.html#API_DescribeLogGroups_Example_1_Request有关头和正文的更多信息。PS:会话令牌是可选的,我不需要它在我的情况下希望它适用于任何人谁

{
    "info": {
        "_postman_id": "8660f3fc-fc6b-4a71-84ba-739d8b4ea7c2",
        "name": "CloudWatch Logs",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "DescribeLogs",
            "request": {
                "auth": {
                    "type": "awsv4",
                    "awsv4": [
                        {
                            "key": "service",
                            "value": "{{AWS_SERVICE_NAME}}",
                            "type": "string"
                        },
                        {
                            "key": "region",
                            "value": "{{AWS_REGION}}",
                            "type": "string"
                        },
                        {
                            "key": "secretKey",
                            "value": "{{AWS_SECRET_ACCESS_KEY}}",
                            "type": "string"
                        },
                        {
                            "key": "accessKey",
                            "value": "{{AWS_ACCESS_KEY_ID}}",
                            "type": "string"
                        },
                        {
                            "key": "sessionToken",
                            "value": "",
                            "type": "string"
                        }
                    ]
                },
                "method": "POST",
                "header": [
                    {
                        "key": "X-Amz-Target",
                        "value": "Logs_20140328.DescribeLogGroups",
                        "type": "text"
                    },
                    {
                        "key": "Content-Encoding",
                        "value": "amz-1.0",
                        "type": "text"
                    }
                ],
                "body": {
                    "mode": "raw",
                    "raw": "{}",
                    "options": {
                        "raw": {
                            "language": "json"
                        }
                    }
                },
                "url": {
                    "raw": "https://{{AWS_SERVICE_NAME}}.{{AWS_REGION}}.amazonaws.com",
                    "protocol": "https",
                    "host": [
                        "{{AWS_SERVICE_NAME}}",
                        "{{AWS_REGION}}",
                        "amazonaws",
                        "com"
                    ]
                }
            },
            "response": []
        }
    ]
}
bpzcxfmw

bpzcxfmw3#

你们帮我找出了我做错了什么,谢谢!现在,我没有尝试下载文件并导入它(上面建议),因为我已经在Postman中设置好了。但是我看到我丢失了标题“Content-Encoding”“amz-1.0”。然后它起作用了。真不敢相信!:)

相关问题