# Help with dynamic filename in Watch URL

**URL:** <https://discuss.elastic.co/t/help-with-dynamic-filename-in-watch-url/100896>\
**Category:** Elasticsearch\
**Created:** [September 18, 2017, 3:52pm UTC](https://discuss.elastic.co/t/help-with-dynamic-filename-in-watch-url/100896 "2017-09-18T15:52:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zen300](https://avatars.discourse-cdn.com/v4/letter/z/7993a0/32.png) [@zen300](https://discuss.elastic.co/u/zen300)\
**Post date:** [September 18, 2017, 3:52pm UTC](https://discuss.elastic.co/t/help-with-dynamic-filename-in-watch-url/100896/1 "2017-09-18T15:52:16Z")

</div>

I browsed a few topics but couldn't find a similar example. I basically want a Watch set up, searches for keyword and emails (as attachment) any files it finds with that keyword. It works if I hardcode the filename in the URL (ie Attachment.pdf), if I try and use mustache templating it errors at Watch creation time with "results in error "Malformed URL [{{ctx.payload.hits.hits.0.\_source.filename}}]""

If I try and use just ctx.payload.hits.hits.0.\_source.filename without the brackets, then it sends that literal text to my webserver, which of course doesn't know what ctx.payload.hits....filename is and my webserver errors.

```
"trigger" : {
   "schedule" : {
      "interval": "12h"
   }
 },
 "input" : {
   "search" : {
     "request" : {
       "body" : {
         "query": {
            "match": { 
               "_all" : "keyword" 
             }
          }
       }
     }
   }
 },
 "condition" : {
   "compare" : { 
     "ctx.payload.hits.total" : { 
       "gt" : 0 
     }
   }
 },
 "actions" : {
   "email_admin" : {
     "email" : {
       "to" : "me@company.com",
       "subject" : "Keyword found!",
       "attachments" : {
         "heaven.pdf" : { 
           "http" : { 
           "content_type" : "application/pdf",
             "request" : {
               "url": "http://localhost:8000/{{ctx.payload.hits.hits.0._source.filename}}"
             }
           }
         }
       }
     }
   }
 }

```

A sample response from my keyword search

```
                "file": {
                    "extension": "pdf",
                    "content_type": "application/pdf",
                    "last_modified": "2017-09-05T11:30:51.605+0000",
                    "indexing_date": "2017-09-12T17:01:22.214+0000",
                    "filesize": 93745,
                    "filename": "7-19-17 CC - Linn Ann Jones Griffin.pdf",
                    "url": "file://c:\\express\\agendakeyword\\BoardDocs\\7-19-17 CC - Linn Ann Jones Griffin.pdf"
                },
                "path": {
                    "root": "7511c68c2ea637f9d0867848d06b7d55",
                    "virtual": "/7-19-17 CC - Linn Ann Jones Griffin.pdf",
                    "real": "c:\\express\\agendakeyword\\BoardDocs\\7-19-17 CC - Linn Ann Jones Griffin.pdf"
                }
            }

```

I don't understand why I'm getting a malformed error. Thanks

---

<div class="post-metadata">

**Author:** ![zen300](https://avatars.discourse-cdn.com/v4/letter/z/7993a0/32.png) [@zen300](https://discuss.elastic.co/u/zen300)\
**Post date:** [September 18, 2017, 4:41pm UTC](https://discuss.elastic.co/t/help-with-dynamic-filename-in-watch-url/100896/2 "2017-09-18T16:41:48Z")

</div>

Apparently the URL field doesn't allow for templating? I switched to this and now it works.

```
 "http" : { 
        "content_type" : "text/plain",
        "request" : {
          "host": "localhost",
          "port": 8000,
          "path": "/{{ctx.payload.hits.hits.0._source.file.filename}}"
        }
      }
    }
```

---

<div class="post-metadata">

**Author:** ![zen300](https://avatars.discourse-cdn.com/v4/letter/z/7993a0/32.png) [@zen300](https://discuss.elastic.co/u/zen300)\
**Post date:** [September 18, 2017, 7:09pm UTC](https://discuss.elastic.co/t/help-with-dynamic-filename-in-watch-url/100896/3 "2017-09-18T19:09:42Z")

</div>

My next question is, is the watcher robust enough to perform a search, then send an email with each attachment (with the attachment filename being dynamically generated). If so, are there any examples of this?

It seems a middle-ware piece would need to be developed, have the watcher setup as webhook that calls another service that then parses the payload and sends out an email. Although I'd like to avoid unnecessary middle-ware pieces. Thanks

```
"input" : {
  "search" : {
    "request" : {
      "body" : {
        "query": {
           "match": { 
              "_all" : "keyword" 
            }
         }
      }
    }
  }
},
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 2, 2017, 1:05pm UTC](https://discuss.elastic.co/t/help-with-dynamic-filename-in-watch-url/100896/4 "2017-10-02T13:05:00Z")

</div>

can you please start including the full watch, and the output of the execute watch API of that watch as well. All those loose snippets make it hard to follow.

Thank you!

--Alex

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 30, 2017, 1:05pm UTC](https://discuss.elastic.co/t/help-with-dynamic-filename-in-watch-url/100896/5 "2017-10-30T13:05:01Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
