0

In my app, I use an api, the production server of which is open, but the dev server of which is protected by htaccess login.

I need to test something that's in the dev server of the api, but can't get around that htaccess wall.

I thought I could put it in the URL, the way you do in a browser such as "https://username:password@example.com" which I realize creates some exposed credential issues, but I can think of a few ways around that.

But no dice. I get 401 unauthorized errors and then when I click the link I got the 401 unauthorized error for, it loads the json of the api perfectly in my browser.

Is there a way to get around an htaccess in retrofit2? My code is below with credentials and variables obscured of course.

my retrofit interface:

public interface DevApi {
    @POST("api/{data0}/{data1}/{data2}")
    fun getMoviesByCategory(@Path("data0") data0: String, @Path("data1") data1: Int, @Path("data2") data2: Int): Single<ArrayList<KMovie>>
}

my interactor class:

class KMovieInteractorImpl : KMovieInteractor {

  @Inject
  lateinit var testAPI: DevApi

  init {
    DaggerMovieInteractorComponent.create().inject(this)
  }

  override fun getGenreMovies(data0: String, data1: Int, data2: Int): Single<ArrayList<KMovie>> {
    return testAPI.getMoviesByCategory(data0, data1, data2).subscribeOn(Schedulers.io())
  }
}

logcat output:

2020-04-13 16:56:33.839 11675-11935/com.myapp D/OkHttp: --> POST https://user:password@dev.api.biz/api/data0/data1/data2
2020-04-13 16:56:33.841 11675-11937/com.myapp D/OkHttp: --> POST user:password@dev.api.biz/api/data00/data1/data2
2020-04-13 16:56:34.153 11675-11937/com.myapp D/OkHttp: <-- 401 Unauthorized user:password@dev.api.biz/api/data0/data1/data2(312ms)
2020-04-13 16:56:34.157 11675-11937/com.myapp D/OkHttp: <address>Apache/2.4.29 (Ubuntu) Server at dev.api.biz Port 443</address>
2020-04-13 16:56:34.178 11675-11935/com.myapp D/OkHttp: <-- 401 Unauthorized https://user:password@dev.api.biz/api/data00/data1/data2 (337ms)
Paxana Non Grata
  • 379
  • 1
  • 7
  • 23
  • iirc this is just basic http authentication. You need to set an `Authorization` header with the value `Basic ` where `` is `username:password` base 64 encoded. – heX Apr 14 '20 at 03:44

1 Answers1

1

Add an Authorization header:

public interface DevApi {
    @POST("api/{data0}/{data1}/{data2}")
    fun getMoviesByCategory(@Header("Authorization") auth: String, 
                            @Path("data0") data0: String, 
                            @Path("data1") data1: Int,                         
                            @Path("data2") data2: Int): Single<ArrayList<KMovie>>
}

And change your base url to dev.api.biz. You can also use the Credentials.basic(user, password) function from OkHttp3 to create the auth header.

heX
  • 720
  • 5
  • 9