0

I'am trying to access to my logs stored on logz.io using api-search that they offer me.

Actually, I can access successfully using curl command as I show:

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"}}}}', 

just like https://github.com/logzio/public-api/tree/master/search said.

However, when I use AWS AppSync api, using HttpResolver datasource with params name:HttpDataSourceTest, type:HTTP and endpoint:https://api.logz.io/v1/search, I defined my schema.grapqhl, the request and response template resolvers:

schema.grapgql

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

Request Template Resolver:

{
    "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": "/"
}  

Response Template Resolver:

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

After several attemps and fails I kept very simple, just ask for TimeStamp field on query and show status and all returned in response.

After all this configurations i get this response:

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

The same result when I skip X-API-TOKEN param header, its like HttpDatasource dont send that params. I am new using all this techs, AWS Services and Logz.io, please tell me if I'm omitting something in some place.

  • I tried to access using a simple angular app: ` export class LogzComponent implements OnInit { result: string; constructor(private http : HttpClient) { } ngOnInit() { let headers = new 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('https://api.logz.io/v1/search', body, {headers:headers}) .subscribe(response => console.log(response)); }}` – Yoan Asdrubal Quintana Ramírez Aug 31 '18 at 16:32
  • THe Options request its OK whit status 200, but after that I got this error: And I got this error. Failed to load https://api.logz.io/v1/search: Request header field X-API-TOKEN is not allowed by Access-Control-Allow-Headers in preflight response – Yoan Asdrubal Quintana Ramírez Aug 31 '18 at 16:36

1 Answers1

0

A single http data source can be used my many resolvers and hit different paths relative to that same root. For this reason when you setup your HTTP data source set the endpoint to https://api.logz.io and then use this for your request mapping template:

{
    "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"
        }
    }
}
mparis
  • 3,623
  • 1
  • 17
  • 16
  • 1
    Thanks you very much, I though it could work defining a full endpoint on httpDataSource configuration, avoiding repeat /v1/search for every RequestTemplateResolver, You just solved me a big problem, I spent a lot of time trying to solve this problem – Yoan Asdrubal Quintana Ramírez Aug 31 '18 at 17:52