1 year ago

#371522

test-img

Abhishek Bansal

Android WebView Post form data request

I am trying to open web-view with given params:

  • URL
  • request-params (token, mid, check_sum_hash)
  • Header (Content-Type "multipart/form-data; boundary=Boundary-${uuid}")
  • Method POST

WebViewClient object

   mBinding.webView.webViewClient = object : WebViewClient() {
            override fun shouldInterceptRequest(
                view: WebView?,
                request: WebResourceRequest?
            ): WebResourceResponse? {
                if (request!=null){
                    val type = "multipart/form-data; boundary=Boundary-${uuid}"
                    request.requestHeaders["Content-Type"] = type
                }
                return super.shouldInterceptRequest(view, request)
            }
        }

    mBinding.webView.settings.javaScriptEnabled = true

Request

        val map: HashMap<String, String> = HashMap()
        map["token"] = getJsonData()
        map["mid"] = BuildConfig.MID
        map["check_sum_hash"] = bookAppointmentSharedData.checkSum!!

        val boundary = "Boundary-${uuid}"
        var body = ""
        for (param in map) {
            body += "--${boundary}\r\n"
            body += "Content-Disposition:form-data; name=\"${param.key}\""
            body += "\r\n\r\n${param.value}\r\n"
        }
        body += "--${boundary}--\r\n";
        val postData = URLEncoder.encode(body, "UTF-8")
        mBinding.webView.postUrl(url, postData.toByteArray())

Web-view is opening page at given url but not accepting params however, it works fine in iOS when i used same param and url with same content-type.

android

kotlin

android-webview

0 Answers

Your Answer

Accepted video resources