I want use request post in my app. I used this but I have a problem. I face this problem when I want to create an account.
status code 403
You can see my code:
Api.kt
interface Api {
@Headers("Content-Type: application/x-www-form-urlencoded")
@POST("users/signup")
@FormUrlEncoded
fun createUser(
@Field("first_name") fName: String,
@Field("last_name") lName: String,
@Field("email") email: String,
@Field("username") username: String,
@Field("password") password: String,
) : Call<UserResponse>
}
UserResponse.kt
data class UserResponse(
val email: String,
val first_name: String,
val password: String,
val last_name: String,
val username: String
)
SignUpActivity.kt
binding.btnSignUp
.setOnClickListener {
val username = binding.etUsername.text.toString().trim()
val password = binding.etPw.text.toString().trim()
val fName = binding.etFName.text.toString().trim()
val lName = binding.etLName.text.toString().trim()
val email = binding.etEmail.text.toString().trim()
if (username.isEmpty()) {
binding.etUsername.error = "Password required"
binding.etUsername.requestFocus()
return@setOnClickListener
}
if (password.isEmpty()) {
binding.etPw.error = "Password required"
binding.etPw.requestFocus()
return@setOnClickListener
}
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY
val httpClient = OkHttpClient.Builder()
httpClient.addInterceptor(logging)
val instance: Api by lazy {
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build()
retrofit.create(Api::class.java)
}
instance.createUser(fName, lName, email, username, password)
.enqueue(object : Callback<UserResponse> {
override fun onFailure(call: Call<UserResponse>, t: Throwable) {
Toast.makeText(applicationContext, t.message, Toast.LENGTH_LONG).show()
}
override fun onResponse(
call: Call<UserResponse>,
response: Response<UserResponse>
) {
Toast.makeText(applicationContext, response.code(), Toast.LENGTH_LONG)
.show()
}
})
}
I use this request on postman and it works good
Happy Coding :-)