0

I have an API call to display search item in recyclerview. So i'm using retrofit and kotlin coroutines for the API call. But the network call returning the forbidden error

{protocol=h2, code=403, message=, url=https://www.blibli.com/backend/search/products?&searchTerm=samsung&start=0&itemPerPage=24}

The url is correct, because i can get response using volley library, but i need to get the response from retrofit.

This is interface class

interface ApiService {

@GET("products")
suspend fun getItems(@Query("searchTerm") item : String,
                     @Query("start") start : String,
                     @Query("itemPerPage") page : String) : Example

}

class SearchViewModel : ViewModel() {

    fun getItems() : LiveData<Example?> {

        System.out.println("========== activated")
        var response = liveData(Dispatchers.IO) {

            try {
                var items = Api.getRetrofit()?.getItems("samsung", "0","24")
                emit(items)
                System.out.println("========== retrofit $items")
            }catch (ex: Exception){}
            catch (ex: Throwable){}
            catch (ex: HttpRetryException){}
        }
        return response
    }
}
class MainActivity : AppCompatActivity() {

    lateinit var viewModel : SearchViewModel
    var adapter : SearchAdapter? = null

    var otherOffer : OtherOfferings? =null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search)

        viewModel = ViewModelProviders.of(this).get(SearchViewModel::class.java)

        viewModel.getItems().observe(this, Observer {

            System.out.println("========= ${it}")
        })

    }
}

Someone please help me, i don't know what's wrong with my code, the same code works with other APIs.

PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • Do you have any headers you are sending out? Does the site you are hitting require any form of authentication or some header that is used for authentication purposes? – PGMacDesign Sep 03 '19 at 15:24
  • no, no headers and authentications. – Muthu Kumar Sep 03 '19 at 15:37
  • Thank you, one other question, where is the code that instantiates the Retrofit client object? It appears as though the path is not being built correctly and it is trying to hit a malformed URL. Can you post the code the Retrofit client object creation + the base URL you are using in it's constructor? – PGMacDesign Sep 03 '19 at 16:22
  • ```object Api { var BASE_URL = "https://www.blibli.com/backend/search/" var retrofit : Retrofit? = null fun getRetrofit() : ApiService? { if (retrofit == null) { retrofit = Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(CoroutineCallAdapterFactory.invoke()) .build() } return retrofit?.create(ApiService::class.java) } ``` – Muthu Kumar Sep 03 '19 at 16:30
  • i have to use user agent with custom name (blibliandroid), so added interceptor but still its not working. I never used user agent before. – Muthu Kumar Sep 03 '19 at 16:33
  • 2 things to try / do. First, change your `BASE_URL` to `https://www.blibli.com/` and then change the `@GET("products")` in your interface to `@GET("/backend/search/products")`. Yes, the extra forward slash is intentional. If that fixes it, great, if not, let me know here and there's another idea you can try. – PGMacDesign Sep 03 '19 at 18:02
  • Ok i will try, do you know how to set the user agent header before hitting the API? – Muthu Kumar Sep 04 '19 at 03:03
  • Yes, just as you mentioned, you utilize a custom interceptor. Sample code can be found here - https://stackoverflow.com/a/27840834/2480714 – PGMacDesign Sep 04 '19 at 15:09
  • 1
    I changed User-Agent value in headers not in interceptors, now i can get the response. – Muthu Kumar Sep 05 '19 at 05:28

0 Answers0