|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * serverless/reference/kn-func-ref.adoc |
| 4 | + |
| 5 | +:_content-type: REFERENCE |
| 6 | +[id="serverless-kn-func-invoke-reference_{context}"] |
| 7 | += kn func invoke optional parameters |
| 8 | + |
| 9 | +You can specify optional parameters for the request by using the following `kn func invoke` CLI command flags. |
| 10 | + |
| 11 | +[options="header",cols="1,3"] |
| 12 | +|=== |
| 13 | +| Flags | Description |
| 14 | +| `-t`, `--target` | Specifies the target instance of the invoked function, for example, `local` or `remote` or `https://staging.example.com/`. The default target is `local`. |
| 15 | +| `-f`, `--format` | Specifies the format of the message, for example, `cloudevent` or `http`. |
| 16 | +| `--id` | Specifies a unique string identifier for the request. |
| 17 | +| `-n`, `--namespace` | Specifies the namespace on the cluster. |
| 18 | +| `--source` | Specifies sender name for the request. This corresponds to the CloudEvent `source` attribute. |
| 19 | +| `--type` | Specifies the type of request, for example, `boson.fn`. This corresponds to the CloudEvent `type` attribute. |
| 20 | +| `--data` | Specifies content for the request. For CloudEvent requests, this is the CloudEvent `data` attribute. |
| 21 | +| `--file` | Specifies path to a local file containing data to be sent. |
| 22 | +| `--content-type` | Specifies the MIME content type for the request. |
| 23 | +| `-p`, `--path` | Specifies path to the project directory. |
| 24 | +| `-c`, `--confirm` | Enables prompting to interactively confirm all options. |
| 25 | +| `-v`, `--verbose` | Enables printing verbose output. |
| 26 | +| `-h`, `--help` | Prints information on usage of `kn func invoke`. |
| 27 | +|=== |
| 28 | + |
| 29 | +[id="serverless-kn-func-invoke-main-parameters_{context}"] |
| 30 | +== Main parameters |
| 31 | + |
| 32 | +The following parameters define the main properties of the `kn func invoke` command: |
| 33 | + |
| 34 | +Event target (`-t`, `--target`):: The target instance of the invoked function. Accepts the `local` value for a locally deployed function, the `remote` value for a remotely deployed function, or a URL for a function deployed to an arbitrary endpoint. If a target is not specified, it defaults to `local`. |
| 35 | +Event message format (`-f`, `--format`):: The message format for the event, such as `http` or `cloudevent`. This defaults to the format of the template that was used when creating the function. |
| 36 | +Event type (`--type`):: The type of event that is sent. You can find information about the `type` parameter that is set in the documentation for each event producer. For example, the API server source might set the `type` parameter of produced events as `dev.knative.apiserver.resource.update`. |
| 37 | +Event source (`--source`):: The unique event source that produced the event. This might be a URI for the event source, for example `https://10.96.0.1/`, or the name of the event source. |
| 38 | +Event ID (`--id`):: A random, unique ID that is created by the event producer. |
| 39 | +Event data (`--data`):: Allows you to specify a `data` value for the event sent by the `kn func invoke` command. For example, you can specify a `--data` value such as `"Hello World"` so that the event contains this data string. By default, no data is included in the events created by `kn func invoke`. |
| 40 | ++ |
| 41 | +[NOTE] |
| 42 | +==== |
| 43 | +Functions that have been deployed to a cluster can respond to events from an existing event source that provides values for properties such as `source` and `type`. These events often have a `data` value in JSON format, which captures the domain specific context of the event. By using the CLI flags noted in this document, developers can simulate those events for local testing. |
| 44 | +==== |
| 45 | ++ |
| 46 | +You can also send event data using the `--file` flag to provide a local file containing data for the event. In this case, specify the content type using `--content-type`. |
| 47 | +Data content type (`--content-type`):: If you are using the `--data` flag to add data for events, you can use the `--content-type` flag to specify what type of data is carried by the event. In the previous example, the data is plain text, so you might specify `kn func invoke --data "Hello world!" --content-type "text/plain"`. |
| 48 | + |
| 49 | +[id="serverless-kn-func-invoke-example-commands_{context}"] |
| 50 | +== Example commands |
| 51 | + |
| 52 | +This is the general invocation of the `kn func invoke` command: |
| 53 | + |
| 54 | +[source,terminal] |
| 55 | +---- |
| 56 | +$ kn func invoke --type <event_type> --source <event_source> --data <event_data> --content-type <content_type> --id <event_ID> --format <format> --namespace <namespace> |
| 57 | +---- |
| 58 | + |
| 59 | +For example, to send a "Hello world!" event, you can run: |
| 60 | + |
| 61 | +[source,terminal] |
| 62 | +---- |
| 63 | +$ kn func invoke --type ping --source example-ping --data "Hello world!" --content-type "text/plain" --id example-ID --format http --namespace my-ns |
| 64 | +---- |
| 65 | + |
| 66 | +[id="serverless-kn-func-invoke-example-commands-specifying-file-with-data_{context}"] |
| 67 | +=== Specifying the file with data |
| 68 | + |
| 69 | +To specify the file on disk that contains the event data, use the `--file` and `--content-type` flags: |
| 70 | + |
| 71 | +[source,terminal] |
| 72 | +---- |
| 73 | +$ kn func invoke --file <path> --content-type <content-type> |
| 74 | +---- |
| 75 | + |
| 76 | +For example, to send JSON data stored in the `test.json` file, use this command: |
| 77 | + |
| 78 | +[source,terminal] |
| 79 | +---- |
| 80 | +$ kn func invoke --file ./test.json --content-type application/json |
| 81 | +---- |
| 82 | + |
| 83 | +[id="serverless-kn-func-invoke-example-commands-specifying-function-project_{context}"] |
| 84 | +=== Specifying the function project |
| 85 | + |
| 86 | +You can specify a path to the function project by using the `--path` flag: |
| 87 | + |
| 88 | +[source,terminal] |
| 89 | +---- |
| 90 | +$ kn func invoke --path <path_to_function> |
| 91 | +---- |
| 92 | + |
| 93 | +For example, to use the function project located in the `./example/example-function` directory, use this command: |
| 94 | + |
| 95 | +[source,terminal] |
| 96 | +---- |
| 97 | +$ kn func invoke --path ./example/example-function |
| 98 | +---- |
| 99 | + |
| 100 | +[id="serverless-kn-func-invoke-example-commands-specifying-where-function-is-deployed_{context}"] |
| 101 | +=== Specifying where the target function is deployed |
| 102 | + |
| 103 | +By default, `kn func invoke` targets the local deployment of the function: |
| 104 | + |
| 105 | +[source,terminal] |
| 106 | +---- |
| 107 | +$ kn func invoke |
| 108 | +---- |
| 109 | + |
| 110 | +To use a different deployment, use the `--target` flag: |
| 111 | + |
| 112 | +[source,terminal] |
| 113 | +---- |
| 114 | +$ kn func invoke --target <target> |
| 115 | +---- |
| 116 | + |
| 117 | +For example, to use the function deployed on the cluster, use the `--target remote` flag: |
| 118 | + |
| 119 | +[source,terminal] |
| 120 | +---- |
| 121 | +$ kn func invoke --target remote |
| 122 | +---- |
| 123 | + |
| 124 | +To use the function deployed at an arbitrary URL, use the `--target <URL>` flag: |
| 125 | + |
| 126 | +[source,terminal] |
| 127 | +---- |
| 128 | +$ kn func invoke --target "https://my-event-broker.example.com" |
| 129 | +---- |
| 130 | + |
| 131 | +You can explicitly target the local deployment. In this case, if the function is not running locally, the command fails: |
| 132 | + |
| 133 | +[source,terminal] |
| 134 | +---- |
| 135 | +$ kn func invoke --target local |
| 136 | +---- |
0 commit comments