Unresolved reference POST request and implementing custom headers/body Kotlin Jetpack Compose Retrofit

56 Views Asked by At

I have errors and questions with the following code:

Data Class:

data class AccountCredentials(
    val email: String,
    val password: String
)

View Model:

class ServerDataViewModel(application: Application) : AndroidViewModel(application) {
    private val _data = MutableLiveData<String>()
    val data: LiveData<String> = _data

    fun createAccount(email: String, password: String) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                val response = RetrofitInstance.api.createAccount(AccountCredentials(email, password))
                if (response.isSuccessful && response.body() != null) {
                    _data.postValue(response.body())
                }
            } catch (e: HttpException) {
            } catch (e: IOException) {
            }
        }
    }
}

Interface:

interface ApiInterface {
    @POST("/create-accouunt")
    suspend fun createAccount(@Body credentials: AccountCredentials): Call<AccountCredentials>
}

For some reason, with a GET request, there's no errors, but with the above POST request I get an error with .isSuccesful and .body(). I'm also unsure as how to set get body and header parameters from the view model.

Edit: What happens with the variable when there's not a @Header or @body declaration in the API Interface? When there is a header or body declaration how do you pass those parameters when calling the function?

0

There are 0 best solutions below