简体   繁体   中英

Getting 401 unauthorized error in retrofit?

Error 401 unauthorized means request is denied due to invalid credentials. I am making request to https URL using retrofit and getting 401 unauthorized while doing curl https://external:userName@api-staging.xyz.com/user/get it works perfectly for me (getting expected output).

There is nothing wrong with the Retrofit implementation in my application because other calls is working fine. Is that something that I cannot pass such URL to retrofit.setEndpoint ?

It is possible that Retrofit does not support putting the username and password in the URL (due to security reasons). This kind of authentication is called Basic Authentication, and you can pass these parameters in an HTTP header as well.

Here's an article about how that can be done in Retrofit: https://futurestud.io/blog/android-basic-authentication-with-retrofit/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM