0

I have the following curl to send GPS data to my odoo server. It worked fine. But when I try doing it in volley kotlin/androidstudio it does not work. Please help.

curl -X POST -H"Content-Type: multipart/form-data" -F coordinates=[4.5232, -96.4242] url

Here is the code snippet I've tried so far:

var lat: Float = intent.getFloatExtra("LATITUDE",0f)
var lng: Float = intent.getFloatExtra("LONGITUDE",0f)
val coordinatesRequest = object: JsonObjectRequest(Request.Method.POST,
                "http://url.com",
                null,
                Response.Listener<JSONObject>{response ->
                    Log.d(TAG,"Response: ${format(response.toString())}")

                },
                Response.ErrorListener { error ->
                    com.android.volley.VolleyLog.e(TAG,"/post request failed! Error: ${error.message}")
                }) {

                //override fun getBodyContentType(): String {
                    //return MULTIPART_FORMDATA
                //}

                override fun getBody(): ByteArray {
                    val params1 = HashMap<String, String>()
                    params1.put("coordinates","[$lat,$lng]")
                    val charset = Charsets.US_ASCII
                    //val map: List<String> = params.map {
                            //(key, value) -> "--$BOUNDARY\nContent-Disposition: form-data; name=\"$key\"\n\n$value\n"
                    //}
                    //val endResult = "${map.joinToString("")}\n--$BOUNDARY--\n"
                    return params1.toString().toByteArray(charset)
                }

                override fun getHeaders(): MutableMap<String, String> {
                    val headers = HashMap<String,String>()
                    headers.put("Content-Type","multipart/form-data")
                    return headers
                }

            }

            Log.d(TAG, "lo que envío: ${coordinatesRequest.body}")
            HttpRequestSingleton.getInstance(context.applicationContext).addToRequestQueue(coordinatesRequest)
        }
Javiseeker
  • 47
  • 1
  • 11

2 Answers2

0

I ran into similar issue with my curl command below -

curl 'https://ws.test.com/handle_email?embed_load_code=undefined' -H 'Accept: /' -H 'Referer: https://cdn1.test.com/widgets/12345/12345/widget.html' -H 'Origin: https://cdn1.test.com' -H 'User-Agent: Test App' -H 'DNT: 1' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' --data 'campaign=12345&merchant=12345&original_data=%257B%2522site%2522%253A%2522site-11111-www.test.com%2522%257D&share_id=&email=test%40test.com' —compressed

Easy way to figure out how to organize the different pieces of cUrl query to Volley Request is by first running this cUrl query in Post Man and intercept request object in Charles Proxy - Ref : Simulate a specific CURL in PostMan

In my case -

-H values were added as Header items --data values are added as body Content-Type set as application/x-www-form-urlencoded

Rams_QA
  • 121
  • 7
  • Thanks, I still could not resolve the issue, but decided to swap to content type json and sent JSONRPC to my server. – Javiseeker Apr 12 '19 at 16:23
0

It worked out with Content-Type:application/json

Javiseeker
  • 47
  • 1
  • 11
  • Volley android has issues with multipart/form-data and thus needs an special extension archive to work properly. I changed the content type in both the server and the android app. – Javiseeker Apr 12 '19 at 16:26