khttp icon indicating copy to clipboard operation
khttp copied to clipboard

Unable to post SOAP messages

Open abbasdgr8 opened this issue 5 years ago • 2 comments

It seems like it is impossible to post any sort of XML content via khttp.post.

On doing the following -

val response = post(
                    url = soapEndpoint,
                    headers = mapOf(
                            "Accept-Encoding" to "gzip,deflate",
                            "Content-Type" to "text/xml;charset=UTF-8"
                    ),
                    data = getSoapRequest()
            )

I receive an error from the server saying Content-Type cannot be text/plain. But, if you look at my POST invocation above, I explicitly set the Content-Type to be text/xml.

I suspected that my Content-Type header was being overwritten somewhere by the khttp library. I investigated into this and I landed up at the following lines of code within the khttp.requests.GenericRequest class on line 45 -

val DEFAULT_DATA_HEADERS = mapOf(
            "Content-Type" to "text/plain"
        )

And on line 134

if (json == null) {
            this.data = data
            if (data != null) {
                mutableHeaders += GenericRequest.DEFAULT_DATA_HEADERS
            }
        }

So it does seem like your Content-Type header always ends up being over-written in case you're posting XML content.

abbasdgr8 avatar May 31 '19 02:05 abbasdgr8

Thank you for helping with the code investigation!

Looking in to get a fix.

bbeaupain avatar Jun 06 '19 16:06 bbeaupain

@bbeaupain I need this in something that I'm using at the moment. Would you be happy for me to take this up and fix it?

abbasdgr8 avatar Oct 09 '20 01:10 abbasdgr8