question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can I set base path to ktor client?

See original GitHub issue

What I expect:

val http = HttpClient(Apache.create()) {
  install(HttpBasePath) {
    basePath = "http://somewhere.in"
  }
}
http.get("/a") // => http://somewhere.in/a
http.get("http://a.b.com") // => http://a.b.com

I tried implementing the above feature, but I couldn’t tell a “localhost” string is the something set or just default.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:33
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
sgarfinkelcommented, Mar 24, 2020

defaultRequest can be used to get this behavior, sort of:

val basepath = Url("https://example.com")

val client = HttpClient {
    defaultRequest {
        url.takeFrom(URLBuilder().takeFrom(basepath).apply {
            encodedPath += url.encodedPath
        })
    }
}

// calls https://example.com/foo/bar
client.get {
    url {
        path("foo", "bar")
    }
}

Note that the url that is in scope in the defaultRequest block is the url defined in the original request–in this case the client.get { } call.

6reactions
gildorcommented, Dec 8, 2018

It would be a very useful feature because most of REST APIs is some host + base path (usually with API version), so you want to extract this part and use for all your reqests that use particular instance of HttpClient Probably make sense to support new property baseUrl/basePath that includes protocol, host and path prefix. IMO it’s the most common way to specify base path. One more open question how to override base path (not base host), maybe we should use the same behavior as Retrofit, which ahas baseUrl (host + basePath). if particular request path starts from / basePath will be ignored (request explicitly uses full path), otherwise basePath will be used

Read more comments on GitHub >

github_iconTop Results From Across the Web

Default request - Ktor
DefaultRequest allows you to configure a base part of the URL that is merged ... You can find the full example here: client-default-request....
Read more >
Can't set a base url that includes path data : KTOR-730
Can 't set a base url that includes path data ; KTOR-697 The DefaultRequest feature overwrites any user specific data. 1 ; KTOR-759...
Read more >
Change Retrofit Base URL on Runtime. | by Narayan Panthi
In this article we will implement a short & simple technique to change retrofit base url. Retrofit is a type-safe HTTP client for...
Read more >
Is there a way to define a base URL in HttpClient By base UR
Is there a way to define a base URL in HttpClient By base URL I mean that all ... feature: https://ktor.io/clients/http-client/features/default-request.html.
Read more >
kTor Android Client Tutorial - Performing Simple GET Request
Tired using Retrofit? Let's try kTor Android Client. In this kTor Android Client Tutorial we will learn performing a simple HTTP GET request ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found