question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not able to get request body from request context

See original GitHub issue

Description

I’m trying to get the body from request context

  • what I’ve tried

inject CurrentVertxRequest context and then get body from there

@Path("/map_event")
public class MapEventApi {

    @Inject
    CurrentVertxRequest reqContext;
  

    @POST
    @Consumes({ "application/json" })
    @Produces({ "application/json" })
    Response create(@Valid MapEvent mapEvent, @Context SecurityContext securityContext) throws Exception {
        String body = reqContext.getCurrent().getBodyAsString();
        ...
    }

}

but the getBodyAsString() gives null value, with a warning:

2022-01-25 18:22:08,854 WARN  [null:-1] (executor-thread-0) BodyHandler in not enabled on this route: RoutingContext.getBodyAsString(...) in always be NULL
  • another try:

inject standard JaxRS HttpServletRequest context

@Context HttpServletRequest

will get this error:

org.jboss.resteasy.spi.LoggableFailure: RESTEASY003880: Unable to find contextual data of type: javax.servlet.http.HttpServletRequest
        at org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy.invoke(ContextParameterInjector.java:155)
        at com.sun.proxy.$Proxy97.getInputStream(Unknown Source)

I guess it’s because quarkus uses vertx under the hood so injecting regular JAX-RS context won’t work since it’s not the same thread.

Can not find a proper way to get the request body, anyone can help?

Implementation ideas

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
voronovmaksimcommented, May 17, 2022

Thanks. We’ll do as you suggested

0reactions
geoandcommented, May 17, 2022

If I needed to read the body many times in filter, what I would do is setup a ContainerRequestFilter with a very low @Priority (thus it would be executed very early) that read the body and saved in a property of ContainerRequestContext. Then my other filters would read the body from there

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get request body of my request - java - Stack Overflow
You could try to wrap the Outputstream for the Entity. First, by using a javax.ws.rs.client.ClientRequestFilter to add a custom Outputstream ...
Read more >
Unable to read HttpContext Body - Microsoft Q&A
Unable to read HttpContext Body. Hi, I am trying to read request information from request body but always getting as empty string.
Read more >
Request and response objects - Django documentation
It's possible that a request can come in via POST with an empty POST dictionary – if, say, a form is requested via...
Read more >
RequestContext (Spring Framework 6.0.2 API)
Suitable for exposition to views, and usage within JSP's "useBean" tag, JSP scriptlets, JSTL EL, etc. Necessary for views that do not have...
Read more >
net/http - Go Packages
Package http provides HTTP client and server implementations. ... New("http: request method or response status code does not allow body") // ErrHijacked is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found