github.com/Jeffail/benthos/v3@v3.65.0/website/docs/guides/sync_responses.md (about) 1 --- 2 title: Synchronous Responses 3 --- 4 5 In a regular Benthos pipeline messages will flow in one direction and acknowledgements flow in the other: 6 7 ```text 8 ----------- Message -------------> 9 10 Input (AMQP) -> Processors -> Output (AMQP) 11 12 <------- Acknowledgement --------- 13 ``` 14 15 However, Benthos has support for a number of protocols where this limitation is not the case. 16 17 For example, HTTP is a request/response protocol, and so our `http_server` input is capable of returning a response payload after consuming a message from a request. 18 19 When using these protocols it's possible to configure Benthos stream pipelines that allow messages to pass in the opposite direction, resulting in response messages at the input level: 20 21 ```text 22 --------- Request Body --------> 23 24 Input (HTTP Server) -> Processors -> Output (Sync Response) 25 26 <--- Response Body (and ack) --- 27 ``` 28 29 ## Routing Processed Messages Back 30 31 It's possible to route the result of any Benthos processing pipeline directly back to an input with a [`sync_response`][sync-res] output: 32 33 ```yaml 34 input: 35 http_server: 36 path: /post 37 pipeline: 38 processors: 39 - bloblang: root = content().uppercase() 40 output: 41 sync_response: {} 42 ``` 43 44 Using the above example, sending a request 'foo bar' to the path `/post` returns the response 'FOO BAR'. 45 46 It's also possible to combine a `sync_response` output with other outputs using a [`broker`][output-broker]: 47 48 ```yaml 49 input: 50 http_server: 51 path: /post 52 output: 53 broker: 54 pattern: fan_out 55 outputs: 56 - kafka: 57 addresses: [ TODO:9092 ] 58 topic: foo_topic 59 - sync_response: {} 60 processors: 61 - bloblang: root = content().uppercase() 62 ``` 63 64 Using the above example, sending a request 'foo bar' to the path `/post` passes the message unchanged to the Kafka topic `foo_topic` and also returns the response 'FOO BAR'. 65 66 :::note 67 It's safe to use these mechanisms even when combining multiple inputs with a broker, a response payload will always be routed back to the original source of the message. 68 ::: 69 70 ## Returning Partially Processed Messages 71 72 It's possible to set the state of a message to be the synchronous response before processing is finished by using the [`sync_response` processor][sync-res-proc]. This allows you to further mutate the payload without changing the response returned to the input: 73 74 ```yaml 75 input: 76 http_server: 77 path: /post 78 79 pipeline: 80 processors: 81 - bloblang: root = "%v baz".format(content().string()) 82 - sync_response: {} 83 - bloblang: root = content().uppercase() 84 85 output: 86 kafka: 87 addresses: [ TODO:9092 ] 88 topic: foo_topic 89 ``` 90 91 Using the above example, sending a request 'foo bar' to the path `/post` passes the message 'FOO BAR BAZ' to the Kafka topic `foo_topic`, and also returns the response 'foo bar baz'. 92 93 However, it is important to keep in mind that due to Benthos' strict delivery guarantees the response message will not actually be returned until the message has reached its output destination and an acknowledgement can be made. 94 95 ## Routing Output Responses Back 96 97 Some outputs, such as [`http_client`][http-client-output], have the potential to propagate payloads received from their destination after sending a message back to the input: 98 99 ```yaml 100 input: 101 http_server: 102 path: /post 103 output: 104 http_client: 105 url: http://localhost:4196/post 106 verb: POST 107 propagate_response: true 108 ``` 109 110 With the above example a message received from the endpoint `/post` would be sent unchanged to the address `http://localhost:4196/post`, and then the response from that request would get returned back. This basically turns Benthos into a proxy server with the potential to mutate payloads between requests. 111 112 The following config turns Benthos into an HTTP proxy server that also sends all request payloads to a Kafka topic: 113 114 ```yaml 115 input: 116 http_server: 117 path: /post 118 output: 119 broker: 120 pattern: fan_out 121 outputs: 122 - kafka: 123 addresses: [ TODO:9092 ] 124 topic: foo_topic 125 - http_client: 126 url: http://localhost:4196/post 127 verb: POST 128 propagate_response: true 129 ``` 130 131 [sync-res]: /docs/components/outputs/sync_response 132 [sync-res-proc]: /docs/components/processors/sync_response 133 [http-client-output]: /docs/components/outputs/http_client 134 [output-broker]: /docs/components/outputs/broker