0

I am developing an android app with kotlin and in this road i want to use a request to respond from restfull server ... Now look at my codes that i request it from server and its true but i don't know how to add the parameters of header to my request

fun sendCode(){
    val URL = "https://RestfulSms.com/api/MessageSend"
    val body : JSONObject = JSONObject()
    val header : JSONObject = JSONObject()
    body.put("Messages",messageText )
    body.put("MobileNumbers",PhoneNumber )
    body.put("LineNumber","***********")
    body.put("SendDateTime","")
    body.put("CanContinueInCaseOfError","false")

    val request :JsonObjectRequest = JsonObjectRequest(Request.Method.POST,
        URL,
        body,
        Response.Listener<JSONObject> { response ->
            Log.e(tag, response.toString())
        },
        Response.ErrorListener{ error ->
            Log.e(tag, error.message )
        })


    //request.headers.put("Content-Type","application/json")
    header.put("x-sms-ir-secure-token",tokenKey)
    queue.getCache().clear();
    queue.add(request)
}
arashforus
  • 25
  • 8
  • Does this answer your question? [How to add custom header in volley request with kotlin](https://stackoverflow.com/questions/51819176/how-to-add-custom-header-in-volley-request-with-kotlin) – Basu Sep 13 '20 at 12:41
  • No dude ... I have a solution for a JsonObjectRequest for set headers – arashforus Sep 13 '20 at 15:42

2 Answers2

1

Please search documentation and other posts before asking such questions. Volley is a standard library and provides a great documentation.

Check this post https://stackoverflow.com/a/44049327/4491971

Mohit Ajwani
  • 1,328
  • 12
  • 24
  • I searched and viewed documentation and other posts before it but i have a little different with other situations ... Others use stringRequest and yes you can override getHeaders functions and replace your custom header , but I use JsonObjectRequest for getting my infos and it doesn't have any getHeaders function to override ... – arashforus Sep 13 '20 at 15:38
  • @arashforus - Can I understand the need for using JsonObjectRequest? If server is expecting it that way, we can apply respective content-type in request instead of doing it this way right? – Mohit Ajwani Sep 15 '20 at 08:19
0

I find my problem and the solution is as like as below

I just replace object with JsonObjectRequest and then use overriding getHeaders function

            val request :JsonObjectRequest = object : JsonObjectRequest(Request.Method.POST,
            URL,
            body,
            Response.Listener<JSONObject> { response ->
                Log.e(tag, response.toString())
            },
            Response.ErrorListener{ error ->
                Log.e(tag, error.message )
            }) {

            @Throws(AuthFailureError::class)
            override fun getHeaders(): Map<String, String> {
                val headers = HashMap<String, String>()
                headers.put("Content-Type", "application/json");
                headers["x-sms-ir-secure-token"] = tokenKey
                return headers
        }
arashforus
  • 25
  • 8