Skip to content

Commit

Permalink
added query param section
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrutiVerma97 committed Feb 1, 2022
1 parent 0dc6453 commit 0a96d00
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions docs/website/docs/v1.x/dsl/request/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

## Creating a Request

`Request` can be created with `method`, `url`, `headers`, `remoteAddress` and `data`.
`Request` can be created with `method`, `url`, `headers`, `remoteAddress` and `data`.
Creating requests using `Request` is useful while writing unit tests.

The below snippet creates a request with default params, `method` as `Method.GET`, `url` as `URL.root`, `headers` as `Headers.empty`, `data` as `HttpData.Empty`, `remoteAddress` as `None`
```scala
Expand Down Expand Up @@ -55,32 +56,18 @@ val app = Http.collect[Request] { case req => Response.text(req.method.toString(
val app = Http.collect[Request] { case req => Response.text(req.url.toString())}
```

## Client Side
`ClientRequest` is designed to create requests for `Client`.
### Creating and reading a Request with query params

## Creating a ClientRequest
Query params can be added in the request using `url` in `Request`, `URL` stores query params as `Map[String, List[String]]`.

`ClientRequest` can be created with `method`, `url`, `getHeaders` and `data`.

The below snippet shows how to create `ClientRequest` using default params
The below snippet creates a request with query params: `?q=a&q=b&q=c`
```scala
val request = Client.ClientRequest(Method.GET, URL(!!))
val request: Request = Request(url = URL(!!, queryParams = Map("q" -> List("a","b","c"))))
```
## Accessing the ClientRequest

- `getBodyAsString` to access the content of request as `Option[String]`
```scala
val request = Client.ClientRequest(Method.GET, URL(!!))
val content: Option[String] = request.getBodyAsString
```
- `remoteAddress` to access the remoteAddress of request as `Option[InetAddress]`
```scala
val request = Client.ClientRequest(Method.GET, URL(!!))
val address: Option[InetAddress] = request.remoteAddress
```
- `updateHeaders` to update headers of request
`url.queryParams` can be used to read query params from the request

The below snippet shows how to read query params from request
```scala
val request = Client.ClientRequest(Method.GET, URL(!!))
val updatedRequest = request.updateHeaders(_.addHeader("name", "value"))
val app = Http.collect[Request] { case req => Response.text(req.url.queryParams.mkString(""))}
```

1 comment on commit 0a96d00

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 932138.33

Please sign in to comment.