In Finagle, how do I create a request an HTTP GET request with parameters in a safe, clean way?
val request = RequestBuilder()
.url("http://www.example.com/test")
.addParameter("key", "value") // this doesn't exist
.buildGet()
request // should be a GET request for http://www.example.com/test?key=value
The modern way of configuring Finagle clients or servers is to use the Finagle 6 API, which is generally available via the with -prefixed methods. For example, the following code snippet creates a new HTTP client altered with two extra parameters: label and transport verbosity.
The expert-level API requires deep knowledge of Finagle internals and it’s recommended to avoid it unless you’re sure of what you’re doing or the corresponding configuration has not yet been exposed through with -prefixed methods.
When the HTTP method is used to request certain resources from the web server, the client also sends some parameters to the web server. These parameters are in a pair of names and their corresponding values, which are called HTTP parameters. Let us now discuss each of them in detail.
To do http get request with parameters in Angular, we can make use of params options argument in HttpClient.get () method. options: { headers?: HttpHeaders | { [header: string]: string | string []}, observe?: 'body' | 'events' | 'response', params?:
You can use the confusingly-named Request.queryString
:
val request = RequestBuilder()
.url(Request.queryString("http://www.example.com/test", Map("key" -> "value"))
.buildGet()
The arguments passed to RequestBuilder.url()
are expected to be fully-formed, so Finagle doesn't provide any way to tack on more query parameters.
One way to solve this would be to construct a URL object outside of the context of the RequestBuilder
chain. You could define your query parameters in a Map
, turn it into a query string, and then concatenate it to the base URL:
val paramStr = Map("key" -> "value", ...) map { case (k, v) =>
k + '=' + v
} mkString("?", "&", "")
val request = RequestBuilder.safeBuildGet(
RequestBuilder.create()
.url("http://www.example.com/test" + paramsStr)
)
Note that you'll need to encode any query parameters that you specify in the Map
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With