Fuel: Force Fuel to encode space as %20 instead of + in url parameters

Created on 6 Apr 2020  路  2Comments  路  Source: kittinunf/fuel

I have a vendor built API that I am accessing that has spaces in some of the url parameters. For some reason on a few of the parameters, if it returns "+" it breaks the call. Is there anyway that I can change the encoding on Fuel to return %20 in the url parameter instead of "+" for a space? Thank you.

Example:

  • Fuel.get("", listOf("keywords" to "John Smith", "service)
  • request.url shows "John+Smith". I need it to show "John%20Smith"

Most helpful comment

that worked perfectly, thank you!

For anyone stumbling on this with the same issue, here is the Interceptor that I used successfully.

kotlin object GetRequestSpaceInterceptor : FoldableRequestInterceptor { override fun invoke(next: RequestTransformer): RequestTransformer { return { request -> request.url = URL(request.url.toString().replace("+", "%20")) next(request) } } }

All 2 comments

You can use an _interceptor_ (Request variant) as shown here. Here you would explicitly transform + to %20, if it's a GET request.

that worked perfectly, thank you!

For anyone stumbling on this with the same issue, here is the Interceptor that I used successfully.

kotlin object GetRequestSpaceInterceptor : FoldableRequestInterceptor { override fun invoke(next: RequestTransformer): RequestTransformer { return { request -> request.url = URL(request.url.toString().replace("+", "%20")) next(request) } } }

Was this page helpful?
0 / 5 - 0 ratings