【问题标题】:How to access to logz api search using appsync httpdatasource如何使用 appsync httpdatasource 访问 logz api 搜索
【发布时间】:2019-02-06 17:23:06
【问题描述】:

我正在尝试使用他们为我提供的 api-search 访问存储在 logz.io 上的日志。

其实我可以使用 curl 命令成功访问,如图所示:

curl -X POST 'https://api.logz.io/v1/search'  
--header "X-API-TOKEN: API-TOKEN-GENERATED" 
--header "Content-Type: application/json" 
-d '{"query": {"term": {"_id": {"value": "Log Id Here"}}}}', 

就像https://github.com/logzio/public-api/tree/master/search 说的那样。

但是,当我使用 AWS AppSync api 时,使用带有参数的 HttpResolver 数据源 名称:HttpDataSourceTest,类型:HTTP 和端点:https://api.logz.io/v1/search,我定义了我的 schema.grapqhl,请求和响应模板解析器:

schema.grapgql

type Query {
    GetLog(id: String): Log
} 

请求模板解析器:

{
    "version": "2018-05-29",
    "method": "POST",
    "params": {
        "headers": {
            "Content-Type: application/json",
            "X-API-TOKEN":"API-TOKEN-GENERATED"
        },
    "body":{
        "query": {
            "term": {
                "_id": {
                    "value": "$context.arguments.id"
                }
             }
         }
    }
    },
    "resourcePath": "/"
}  

响应模板解析器:

$utils.toJson({"TimeStamp":"$ctx.result.statusCode $ctx.result.body" })

经过几次尝试和失败后,我保持非常简单,只需在查询中询问 TimeStamp 字段并显示状态并全部返回响应。

在所有这些配置之后,我得到这个响应:

{
    "data": {
        "GetLog": {
            "TimeStamp": "403 {\"message\":\"Forbidden\"}"
        }
    }
}

当我跳过 X-API-TOKEN 参数标头时,结果相同,就像 HttpDatasource 不发送该参数一样。 我是使用所有这些技术、AWS 服务和 Logz.io 的新手,如果我在某些地方遗漏了什么,请告诉我。

【问题讨论】:

  • 我尝试使用一个简单的 Angular 应用程序进行访问:` export class LogzComponent implements OnInit { result: string;构造函数(私有http:HttpClient){}ngOnInit(){让标头=新的HttpHeaders(); headers = headers .set("Content-Type","application/json") .set("X-API-TOKEN","API TOKEN HERE"); const body = {"query": {"term": {"_id": {"value": "LOG ID"}}}}; this.http.post('api.logz.io/v1/search', body, {headers:headers}) .subscribe(response => console.log(response)); }}`
  • 选项请求其 OK whit 状态 200,但之后我收到此错误:我收到此错误。无法加载api.logz.io/v1/search:预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 X-API-TOKEN

标签: graphql aws-appsync logz.io


【解决方案1】:

一个 http 数据源可以用于我的许多解析器,并针对同一个根访问不同的路径。因此,当您设置 HTTP 数据源时,请将端点设置为 https://api.logz.io,然后将其用于您的请求映射模板:

{
    "version": "2018-05-29",
    "method": "POST",
    ## E.G. if full path is https://api.xxxxxxxxx.com/posts then resourcePath would be /posts **
    "resourcePath": "/v1/search",
    "params":{
        "body":{
          "query": {
            "term": {
              "_id": {
                "value": "$context.arguments.id"
              }
            }
          }
        },
        "headers":{
            "Content-Type": "application/json",
            "X-API-TOKEN":"API-TOKEN-GENERATED"
        }
    }
}

【讨论】:

  • 非常感谢,虽然它可以在httpDataSource配置上定义一个完整的端点,避免重复/v1/search每个RequestTemplateResolver,你刚刚解决了我一个大问题,我花了很多时间试图解决这个问题
猜你喜欢
  • 2013-05-28
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
相关资源
最近更新 更多