Home > Blockchain >  GET request working from Postman but not in Android and the browser
GET request working from Postman but not in Android and the browser

Time:10-23

Porblem: I'm working with an api, and when I'm using postman to do a GET request, works perfect and I get the response as I was expecting. But, when I test the same get request in the browser and in Android Studio, doesn't works.

Postaman snippet:

https://i.stack.imgur.com/b2eeS.png

Postman headers:

Content-Type: application/json

User: Dermacycle2018

Password: (its a large password)

Device: Iphone7

VersionSO: IOS 11.2.2

VersionAPP: 1.0.1

Postman-Token: calculated when request is sent

Host: calculated when request sent

User-Agent: PostmanRuntime/7.28.2

Accept: /

Accept-Encoding: gzip,deflate,br

Connection: keep-alive

Browser snippet:

https://i.stack.imgur.com/6KjpW.png

Android code:

here I am using retrofit to process the response

interface DermaCycleApiClient {

    @GET
    suspend fun getTratamientosFaciales(@Url url:String)
    :Response<TratamientosFacialesResponse>

}

fun getRetrofit():Retrofit{
        return Retrofit.Builder().
        baseUrl("http://dermaservice.theappmaster.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .build()
    }
suspend fun getTratamientosFaciales(tf:String):TratamientosFacialesResponse{
        return withContext(Dispatchers.IO){
            val response = retrofit.create(DermaCycleApiClient::class.java).
            getTratamientosFaciales("http://dermaservice.theappmaster.com/Tratamiento.ashx?Tipo=TF")
 
            response.body()!!

        }

    }

CodePudding user response:

I believe your HTTP request require several Headers information as can be seen from your postman data. Seems, there are 5 header values you need to add in your HTTP client header in your Android project

User: Dermacycle2018
Password: (its a large password)
Device: Iphone7
VersionSO: IOS 11.2.2
VersionAPP: 1.0.1

You can add it in your OkHttpClient builder by using an Interceptor.

class HeaderInterceptor: Interceptor {
    
    override fun intercept(chain: Interceptor.Chain): Response {
        val newRequest = chain.request().newBuilder()
             .addHeader("User", "Dermacycle2018")
             .addHeader("Password", ENCRYPTED_PASSWORD)
             .addHeader("Device", DEVICE_NAME)
             .addHeader("VersionSO", OS_VERSION)
             .addHeader("VersionAPP", "1.0.1")
        return chain.proceed(newRequest.build())
    }
}

and then add the interceptor in your OkHttpClient builder

val okHttpClient = OkHttpClient().newBuilder()
    .addInterceptor(commonHeader)
    .build

then add okHttpClient in your retrofit builder

fun getRetrofit(): Retrofit {
    val okHttpClient = OkHttpClient().newBuilder()
        .addInterceptor(commonHeader)
        .build

    return Retrofit.Builder()
        .baseUrl("http://dermaservice.theappmaster.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .client(okHttpClient)
        .build()
    }
  • Related