Search code examples
androidpostparametersandroid-jetpack-composeandroid-volley

Android Compose Volley Post Request with parameters


I need make request in Android Kotlin Compose with Volley. The request is in POST method with 2 parameters: email and token. I made a simple request but i don't know how to add parameters. Can someone help me? Here

fun PostVolleyRequest(
    context: Context,
    result: MutableState<String>
) {

    val queue = Volley.newRequestQueue(context)
    val email = "[email protected]"

    val url = "http://127.0.0.1:8000/logged/accedi"
    val stringRequest = StringRequest(
        Method.POST,
        url,
        { response -> result.value = response },
        { println("That didn't work!") }
        )   


    queue.add(stringRequest)
}

Solution

  • you can override the getParams() method in the StringRequest class.

    fun PostVolleyRequest(
        context: Context,
        result: MutableState<String>
    ) {
        
        val queue = Volley.newRequestQueue(context)
        val email = "[email protected]"
        val token = "your_token" // replace with your token
        
        val url = "http://127.0.0.1:8000/logged/accedi"
        val stringRequest = object : StringRequest(Method.POST, url, 
            { response -> result.value = response }, { println("That didn't work!") }) {
    
            override fun getParams(): Map<String, String> {
                val params = HashMap<String, String>()
                params["email"] = email
                params["token"] = token
                return params
            }
        }
        
        queue.add(stringRequest)
    }