Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore Content-Type #182

Open
skhro87 opened this issue Nov 1, 2018 · 4 comments
Open

Ignore Content-Type #182

skhro87 opened this issue Nov 1, 2018 · 4 comments

Comments

@skhro87
Copy link

skhro87 commented Nov 1, 2018

My goal is skip the build-in body parsing, and parse the body myself, always assuming a json body, ignoring the actual request Content-Type.
I already set a custom jsonRequestBodyParser and therefore skip the build-in parsing.

However, I'm seeing exceptions in my logs when the Content-Type is empty or plain/text, or basically anything except application/json or application/xml:

java.lang.RuntimeException: Couldn't parse the request body - unsupported content type: text/plain
	at org.rapidoid.u.U.rte(U.java:423)
	at org.rapidoid.http.impl.ReqImpl.parseRequestBody(ReqImpl.java:812)
	at org.rapidoid.http.impl.ReqImpl.posted(ReqImpl.java:244)
	at org.rapidoid.http.impl.ReqImpl.posted(ReqImpl.java:330)
	at org.rapidoid.http.impl.ReqImpl.data(ReqImpl.java:379)
	at org.rapidoid.http.HttpUtils.initAndDeserializeToken(HttpUtils.java:81)
	at org.rapidoid.http.impl.ReqImpl.token(ReqImpl.java:694)
	at org.rapidoid.http.impl.ReqImpl.hasToken(ReqImpl.java:682)
	at org.rapidoid.http.HttpUtils.getAuth(HttpUtils.java:306)
	at org.rapidoid.http.handler.HttpAuthWrapper.wrap(HttpAuthWrapper.java:52)
	at org.rapidoid.http.handler.HttpManagedHandlerDecorator.wrap(HttpManagedHandlerDecorator.java:185)
	at org.rapidoid.http.handler.HttpManagedHandlerDecorator.handleWithWrappers(HttpManagedHandlerDecorator.java:100)
	at org.rapidoid.http.handler.HttpManagedHandlerDecorator.access$200(HttpManagedHandlerDecorator.java:39)
	at org.rapidoid.http.handler.HttpManagedHandlerDecorator$2.run(HttpManagedHandlerDecorator.java:83)
	at org.rapidoid.job.PredefinedContextJobWrapper.run(PredefinedContextJobWrapper.java:56)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

which originates in ReqImpl.java:

private void parseRequestBody() {
		if (U.isEmpty(body())) return;

		String contentTypeHeader = header("Content-Type", "application/json");

		if (contentTypeHeader.startsWith("application/json")) {
			parseRequestBodyUsing(custom().jsonRequestBodyParser());

		} else if (contentTypeHeader.startsWith("application/xml")) {
			parseRequestBodyUsing(custom().xmlRequestBodyParser());

		} else {
			throw U.rte("Couldn't parse the request body - unsupported content type: " + contentTypeHeader);
		}
	}

I tried to have an interceptor overriding the Content-Type (Kotlin code), but to no success, as the interceptor is only run after the body parsing:

val contentTypeMiddleware = HttpWrapper { req, next ->
        req.headers()["Content-Type"] = "application/json"
        next.invoke()
    }
On.defaults().wrappers(contentTypeMiddleware)

Any way I can achieve ignoring the Content-Type and not throwing the exception?
The API still works and is functional, but I can't deploy anything that throws exceptions eventually.

Thank you for your help and the great work on this project!

@lthoulon-locala
Copy link

Hi,

I'm not sure this will fully answer your issue as i myself having occasional errors like that one but if you deactivate managed mode you should be able to handle whatever comes at you:
On.defaults().managed(false);

I'm actually on some occasions having that exception even with managed mode deactivated but I'm only seeing that in production and am unable to reproduce it at the moment.

@skhro87
Copy link
Author

skhro87 commented Jan 17, 2019

Hi Laurent,

sorry for the late reply.
Thank you for the suggestion, but it's not exactly what I was looking for.
Even if it would solve the exception-throwing issue, turning off the managed mode has all kind of other implications (see https://www.rapidoid.org/docs.html#_managed_vs_unmanaged_request_handlers).
I want to keep using the rapidoid managed features and just want to get rid of the exception.

@bokesan
Copy link

bokesan commented Feb 27, 2019

I have come across the same problem: rapidoid seems to parse the request body as JSON even though I only use req.body() to get the raw content. Changing the content type to "text/plain" or "application/octet-stream" results in "unsupported content type".
This is rather limiting and can't be good for performance.

@bgweber
Copy link

bgweber commented Aug 7, 2020

Reverting to version 5.4.6 fixes this issue. It looks like the request body parsing was added in version 5.5.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants