cronet-transport-for-okhttp icon indicating copy to clipboard operation
cronet-transport-for-okhttp copied to clipboard

415 Error When Setting Content-Type Header with Request Header

Open madhurkapadia opened this issue 1 year ago • 8 comments

Description: I'm encountering a 415 (Unsupported Media Type) error when using Cronet to send requests with a request body and an explicitly set Content-Type header

Steps to Reproduce: Create an OkHttp Request object with a non-empty RequestBody and set the "Content-Type" header to "application/json". Use the Cronet library to send the request. Observe a 415 error response from the server.

Expected Behavior: The server should accept the request with the specified Content-Type and process the JSON payload.

Actual Behavior: The server returns a 415 error, indicating that it doesn't support the provided Content-Type.

RequestResponseConverter class has

if (body.contentLength() != 0L) {
                if (okHttpRequest.header("Content-Type") == null && body.contentType() != null) {
                    builder.addHeader("Content-Type", body.contentType().toString());
                } else {
                    builder.addHeader("Content-Type", "application/octet-stream");
                }

                builder.setUploadDataProvider(this.requestBodyConverter.convertRequestBody(body, writeTimeoutMillis), this.uploadDataProviderExecutor);
            }

As the above check is incorrect and shouldn't replace Content-Type header with application/octet-stream blindly. It should respect whatever user is setting.

madhurkapadia avatar Jul 22 '24 10:07 madhurkapadia

can see in new code it has been already fixed

madhurkapadia avatar Jul 22 '24 11:07 madhurkapadia

Given the current issues, when are you planning to release an updated version?

madhurkapadia avatar Jul 22 '24 17:07 madhurkapadia

Currently blocked on using cronet because of this. Any response?

Nek-12 avatar Sep 15 '24 10:09 Nek-12

any updates on this? when are you planning to release?

juanfelippopeya avatar Nov 25 '24 12:11 juanfelippopeya

@Danstahrg @google-admin any updates ? Like Commit 0a43782, It seems that the version in the Maven repository hasn't been updated for a long time.

yevon-cn avatar Nov 25 '24 14:11 yevon-cn

same error, pls update the version

xckevin avatar Mar 14 '25 07:03 xckevin

I added an interceptor to avoid the bug😂

            if (request.body?.contentType() != null) {
                val headerContentType = request.header("Content-Type")
                if (!headerContentType.isNullOrEmpty()) {
                    request = request.newBuilder().removeHeader("Content-Type").build()
                }
            }
            val resp = chain.proceed(request)
            return resp

xckevin avatar Mar 26 '25 02:03 xckevin

@yevon-cn - Thank you for the report. I don't work at Google anymore so I can't be of much help unfortunately.

Danstahrm avatar Jun 17 '25 06:06 Danstahrm