# Extracting raw query from request

**URL:** <https://softwaremill.community/t/extracting-raw-query-from-request/370>\
**Category:** tapir\
**Created:** [February 23, 2024, 4:14pm UTC](https://softwaremill.community/t/extracting-raw-query-from-request/370 "2024-02-23T16:14:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![michalbogacz](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/michalbogacz/32/229_2.png) [@michalbogacz](https://softwaremill.community/u/michalbogacz)\
**Post date:** [February 23, 2024, 4:14pm UTC](https://softwaremill.community/t/extracting-raw-query-from-request/370/1 "2024-02-23T16:14:17Z")

</div>

Hello,

Is it possible to extract whole raw query from request?  
I need original query for very specific auth.

I tried using “extractFromRequest” but this changes encoding.  
E.g. “q=CUS820038%20MILA01060” is changed to “q=CUS820038+MILA01060”.

---

<div class="post-metadata">

**Author:** ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)\
**Post date:** [February 23, 2024, 4:31pm UTC](https://softwaremill.community/t/extracting-raw-query-from-request/370/2 "2024-02-23T16:31:19Z")

</div>

Which backend are you using? Some underlying servers expose the unparsed data; the original request object is available through `extractFromRequest(_.underlying.(...))`, and then depending on the specific interpreter you’ll need a cast

---

<div class="post-metadata">

**Author:** ![michalbogacz](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/michalbogacz/32/229_2.png) [@michalbogacz](https://softwaremill.community/u/michalbogacz)\
**Post date:** [February 23, 2024, 6:15pm UTC](https://softwaremill.community/t/extracting-raw-query-from-request/370/3 "2024-02-23T18:15:50Z")

</div>

Thanks! I’m using Akka Http and this worked

```auto
request.underlying.asInstanceOf[RequestContext].request.uri.rawQueryString

```

---

<div class="post-metadata">

**Author:** ![gdispada](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/gdispada/32/241_2.png) [@gdispada](https://softwaremill.community/u/gdispada)\
**Post date:** [February 24, 2024, 5:13pm UTC](https://softwaremill.community/t/extracting-raw-query-from-request/370/4 "2024-02-24T17:13:13Z")

</div>

Very interesting, I didn’t know about `extractFromRequest` at all

I’m currently working on a functionaly to store both request and responses for a specific endpoint in an external database (a sort of auditing system for financial transactions), and thus far the best option I’ve found is using an endpoint interceptor, and then casting request and response.

Now I wonder if there’s a better way for this use-case

---

<div class="post-metadata">

**Author:** ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)\
**Post date:** [February 25, 2024, 11:23am UTC](https://softwaremill.community/t/extracting-raw-query-from-request/370/5 "2024-02-25T11:23:12Z")

</div>

@gdispada an interceptor (or more specifically, a `RequestInterceptor`) is I think the best tool for this job. Quite possible, you might not have to cast anything - the `ServerRequest` exposes most necessary information (including the URI, although in a parsed form), and the `RequestResult` has the response (if any). You might need some interpreter-specific logic if you need to somehow transform/log the body, but that might have performance implications.

---

<div class="post-metadata">

**Author:** ![gdispada](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/gdispada/32/241_2.png) [@gdispada](https://softwaremill.community/u/gdispada)\
**Post date:** [February 25, 2024, 11:40am UTC](https://softwaremill.community/t/extracting-raw-query-from-request/370/6 "2024-02-25T11:40:51Z")

</div>

@adamw why a request interceptor instead of a endpoint interceptor?

To be honest the documentation I found is not very detailed and I wasn’t able to find any examples (under `examples`), but I thought the endpoint one would be better suited since

- I only want to apply the interceptor to a specific endpoint (or list of endpoints \< all), which is easy to do with endpoint interceptors as I have `context.endpoint`. With a request interceptor, I’d have to match on the request uri, which is less refactor-friendly and error prone
- I do need to store the response body, despite the performance implications

I think that in fact _both_ can work, I can access the response from the `RequestInterceptor` as well

---

<div class="post-metadata">

**Author:** ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)\
**Post date:** [February 25, 2024, 7:32pm UTC](https://softwaremill.community/t/extracting-raw-query-from-request/370/7 "2024-02-25T19:32:02Z")

</div>

An endpoint interceptor might be called multiple times, for each endpoint that fails to decode first, and then for the endpoint that succeeds. Typically though, only one endpoint is attempted to be decoded, due to path pre-matching.

A request interceptor, on the other hand, is always only called once per request.

But if, as you write, you want to enable this conditionally, then yes, an endpoint interceptor will be the better choice, as you have access to the specific endpoint & its attributes (which are great for storing such flags). There, you should probably ignore the `onDecodeFailure` callback, and implement the other two.
