You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Michael Rambichler <mi...@rambichler.at> on 2022/05/10 13:46:13 UTC

Redirect with POST call not possible

Hi Team,

I hope someone can help us.

We have to do a simple rest callout with POST method and http
as producercomponent:
If we use POST as method, the redirect in the http component is not
fulfilled. (If we use GET it works)

After some investigation we realized that we have to configure the
http-client to use LaxRedirectStrategy
But I have no idea how to access the client within camel.

Do you have any ideas?

Simple TestRoute:

protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {

                restConfiguration()
                        .producerComponent("http")
                        .host("localhost")
                        .port((Integer) parameters.get("port"));

                from("direct:start")

.to("rest:POST:users/{id}/basic?throwExceptionOnFailure=false")
                        .log(LoggingLevel.INFO, "Received body : ${body}")
                        .to("mock:result");

            }
        };


@Override
@BeforeEach
public void setUp() throws Exception {
    super.setUp();

    redirectServer = ServerBootstrap.bootstrap()
            .setListenerPort((Integer)parameters.get("port2"))
            .setHttpProcessor(getHttpProcessor())
            .registerHandler("/*", new SimpleHandler("Donald Duck"))
            .create();
    redirectServer.start();

    localServer = ServerBootstrap.bootstrap()
            .setListenerPort((Integer)parameters.get("port"))
            .setHttpProcessor(getHttpProcessor())
            .registerHandler("/users/*", redirectTo(302,
"/redirected", redirectServer))
            .create();
    localServer.start();

Re: Redirect with POST call not possible

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Good to know and thanks for posting the working code.
I created a ticket about adding a new option
https://issues.apache.org/jira/browse/CAMEL-18093

On Tue, May 10, 2022 at 4:58 PM Michael Rambichler
<mi...@rambichler.at> wrote:
>
> Thanks!!!
> With your hint, I have found the solution: httpClientConfigure
>
> from("direct:start")
>         .to("rest:POST:users/{id}/basic?throwExceptionOnFailure=false&httpClientConfigurer=#customConfigurer")
>         .log(LoggingLevel.INFO, "Received body : ${body}")
>         .to("mock:result");
>
> @BindToRegistry("customConfigurer")
> private TestClientConfigurer testConfigurer;
>
> private static class TestClientConfigurer implements HttpClientConfigurer {
>
>     @Override    public void configureHttpClient(HttpClientBuilder
> clientBuilder) {
>         clientBuilder.setRedirectStrategy(new LaxRedirectStrategy());    }
> }
>
>
> Am Di., 10. Mai 2022 um 16:36 Uhr schrieb Claus Ibsen <claus.ibsen@gmail.com
> >:
>
> > Hi
> >
> > I guess there are 2 things
> >
> > 1)
> > To make it easy in camel-http to turn on follow direct out of the box.
> > You are welcome to create a JIRA
> >
> > 2)
> > You can maybe use the http client configurer thingy on camel-http to
> > customize the created client to set that code.
> >
> > On Tue, May 10, 2022 at 4:20 PM Michael Rambichler
> > <mi...@rambichler.at> wrote:
> > >
> > > Hi,
> > >
> > > Yes, exactly. I debugged the http-client code already. And the solution
> > is
> > > to define the redirect strategy:
> > > via: client.setRedirectStrategy(new LaxRedirectStrategy());
> > >
> > > But i do not know how to access the http-client used by the camel-http
> > >
> > > Am Di., 10. Mai 2022 um 16:03 Uhr schrieb Claus Ibsen <
> > claus.ibsen@gmail.com
> > > >:
> > >
> > > > Hi
> > > >
> > > > So are you asking for how to configure the http client to follow
> > redirects?
> > > >
> > > > You use camel-http, so you need to find out how it does redirects
> > > >
> > > > On Tue, May 10, 2022 at 3:46 PM Michael Rambichler
> > > > <mi...@rambichler.at> wrote:
> > > > >
> > > > > Hi Team,
> > > > >
> > > > > I hope someone can help us.
> > > > >
> > > > > We have to do a simple rest callout with POST method and http
> > > > > as producercomponent:
> > > > > If we use POST as method, the redirect in the http component is not
> > > > > fulfilled. (If we use GET it works)
> > > > >
> > > > > After some investigation we realized that we have to configure the
> > > > > http-client to use LaxRedirectStrategy
> > > > > But I have no idea how to access the client within camel.
> > > > >
> > > > > Do you have any ideas?
> > > > >
> > > > > Simple TestRoute:
> > > > >
> > > > > protected RouteBuilder createRouteBuilder() throws Exception {
> > > > >         return new RouteBuilder() {
> > > > >             @Override
> > > > >             public void configure() throws Exception {
> > > > >
> > > > >                 restConfiguration()
> > > > >                         .producerComponent("http")
> > > > >                         .host("localhost")
> > > > >                         .port((Integer) parameters.get("port"));
> > > > >
> > > > >                 from("direct:start")
> > > > >
> > > > > .to("rest:POST:users/{id}/basic?throwExceptionOnFailure=false")
> > > > >                         .log(LoggingLevel.INFO, "Received body :
> > > > ${body}")
> > > > >                         .to("mock:result");
> > > > >
> > > > >             }
> > > > >         };
> > > > >
> > > > >
> > > > > @Override
> > > > > @BeforeEach
> > > > > public void setUp() throws Exception {
> > > > >     super.setUp();
> > > > >
> > > > >     redirectServer = ServerBootstrap.bootstrap()
> > > > >             .setListenerPort((Integer)parameters.get("port2"))
> > > > >             .setHttpProcessor(getHttpProcessor())
> > > > >             .registerHandler("/*", new SimpleHandler("Donald Duck"))
> > > > >             .create();
> > > > >     redirectServer.start();
> > > > >
> > > > >     localServer = ServerBootstrap.bootstrap()
> > > > >             .setListenerPort((Integer)parameters.get("port"))
> > > > >             .setHttpProcessor(getHttpProcessor())
> > > > >             .registerHandler("/users/*", redirectTo(302,
> > > > > "/redirected", redirectServer))
> > > > >             .create();
> > > > >     localServer.start();
> > > >
> > > >
> > > >
> > > > --
> > > > Claus Ibsen
> > > > -----------------
> > > > http://davsclaus.com @davsclaus
> > > > Camel in Action 2: https://www.manning.com/ibsen2
> > > >
> >
> >
> >
> > --
> > Claus Ibsen
> > -----------------
> > http://davsclaus.com @davsclaus
> > Camel in Action 2: https://www.manning.com/ibsen2
> >



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: Redirect with POST call not possible

Posted by Michael Rambichler <mi...@rambichler.at>.
Thanks!!!
With your hint, I have found the solution: httpClientConfigure

from("direct:start")
        .to("rest:POST:users/{id}/basic?throwExceptionOnFailure=false&httpClientConfigurer=#customConfigurer")
        .log(LoggingLevel.INFO, "Received body : ${body}")
        .to("mock:result");

@BindToRegistry("customConfigurer")
private TestClientConfigurer testConfigurer;

private static class TestClientConfigurer implements HttpClientConfigurer {

    @Override    public void configureHttpClient(HttpClientBuilder
clientBuilder) {
        clientBuilder.setRedirectStrategy(new LaxRedirectStrategy());    }
}


Am Di., 10. Mai 2022 um 16:36 Uhr schrieb Claus Ibsen <claus.ibsen@gmail.com
>:

> Hi
>
> I guess there are 2 things
>
> 1)
> To make it easy in camel-http to turn on follow direct out of the box.
> You are welcome to create a JIRA
>
> 2)
> You can maybe use the http client configurer thingy on camel-http to
> customize the created client to set that code.
>
> On Tue, May 10, 2022 at 4:20 PM Michael Rambichler
> <mi...@rambichler.at> wrote:
> >
> > Hi,
> >
> > Yes, exactly. I debugged the http-client code already. And the solution
> is
> > to define the redirect strategy:
> > via: client.setRedirectStrategy(new LaxRedirectStrategy());
> >
> > But i do not know how to access the http-client used by the camel-http
> >
> > Am Di., 10. Mai 2022 um 16:03 Uhr schrieb Claus Ibsen <
> claus.ibsen@gmail.com
> > >:
> >
> > > Hi
> > >
> > > So are you asking for how to configure the http client to follow
> redirects?
> > >
> > > You use camel-http, so you need to find out how it does redirects
> > >
> > > On Tue, May 10, 2022 at 3:46 PM Michael Rambichler
> > > <mi...@rambichler.at> wrote:
> > > >
> > > > Hi Team,
> > > >
> > > > I hope someone can help us.
> > > >
> > > > We have to do a simple rest callout with POST method and http
> > > > as producercomponent:
> > > > If we use POST as method, the redirect in the http component is not
> > > > fulfilled. (If we use GET it works)
> > > >
> > > > After some investigation we realized that we have to configure the
> > > > http-client to use LaxRedirectStrategy
> > > > But I have no idea how to access the client within camel.
> > > >
> > > > Do you have any ideas?
> > > >
> > > > Simple TestRoute:
> > > >
> > > > protected RouteBuilder createRouteBuilder() throws Exception {
> > > >         return new RouteBuilder() {
> > > >             @Override
> > > >             public void configure() throws Exception {
> > > >
> > > >                 restConfiguration()
> > > >                         .producerComponent("http")
> > > >                         .host("localhost")
> > > >                         .port((Integer) parameters.get("port"));
> > > >
> > > >                 from("direct:start")
> > > >
> > > > .to("rest:POST:users/{id}/basic?throwExceptionOnFailure=false")
> > > >                         .log(LoggingLevel.INFO, "Received body :
> > > ${body}")
> > > >                         .to("mock:result");
> > > >
> > > >             }
> > > >         };
> > > >
> > > >
> > > > @Override
> > > > @BeforeEach
> > > > public void setUp() throws Exception {
> > > >     super.setUp();
> > > >
> > > >     redirectServer = ServerBootstrap.bootstrap()
> > > >             .setListenerPort((Integer)parameters.get("port2"))
> > > >             .setHttpProcessor(getHttpProcessor())
> > > >             .registerHandler("/*", new SimpleHandler("Donald Duck"))
> > > >             .create();
> > > >     redirectServer.start();
> > > >
> > > >     localServer = ServerBootstrap.bootstrap()
> > > >             .setListenerPort((Integer)parameters.get("port"))
> > > >             .setHttpProcessor(getHttpProcessor())
> > > >             .registerHandler("/users/*", redirectTo(302,
> > > > "/redirected", redirectServer))
> > > >             .create();
> > > >     localServer.start();
> > >
> > >
> > >
> > > --
> > > Claus Ibsen
> > > -----------------
> > > http://davsclaus.com @davsclaus
> > > Camel in Action 2: https://www.manning.com/ibsen2
> > >
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
>

Re: Redirect with POST call not possible

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

I guess there are 2 things

1)
To make it easy in camel-http to turn on follow direct out of the box.
You are welcome to create a JIRA

2)
You can maybe use the http client configurer thingy on camel-http to
customize the created client to set that code.

On Tue, May 10, 2022 at 4:20 PM Michael Rambichler
<mi...@rambichler.at> wrote:
>
> Hi,
>
> Yes, exactly. I debugged the http-client code already. And the solution is
> to define the redirect strategy:
> via: client.setRedirectStrategy(new LaxRedirectStrategy());
>
> But i do not know how to access the http-client used by the camel-http
>
> Am Di., 10. Mai 2022 um 16:03 Uhr schrieb Claus Ibsen <claus.ibsen@gmail.com
> >:
>
> > Hi
> >
> > So are you asking for how to configure the http client to follow redirects?
> >
> > You use camel-http, so you need to find out how it does redirects
> >
> > On Tue, May 10, 2022 at 3:46 PM Michael Rambichler
> > <mi...@rambichler.at> wrote:
> > >
> > > Hi Team,
> > >
> > > I hope someone can help us.
> > >
> > > We have to do a simple rest callout with POST method and http
> > > as producercomponent:
> > > If we use POST as method, the redirect in the http component is not
> > > fulfilled. (If we use GET it works)
> > >
> > > After some investigation we realized that we have to configure the
> > > http-client to use LaxRedirectStrategy
> > > But I have no idea how to access the client within camel.
> > >
> > > Do you have any ideas?
> > >
> > > Simple TestRoute:
> > >
> > > protected RouteBuilder createRouteBuilder() throws Exception {
> > >         return new RouteBuilder() {
> > >             @Override
> > >             public void configure() throws Exception {
> > >
> > >                 restConfiguration()
> > >                         .producerComponent("http")
> > >                         .host("localhost")
> > >                         .port((Integer) parameters.get("port"));
> > >
> > >                 from("direct:start")
> > >
> > > .to("rest:POST:users/{id}/basic?throwExceptionOnFailure=false")
> > >                         .log(LoggingLevel.INFO, "Received body :
> > ${body}")
> > >                         .to("mock:result");
> > >
> > >             }
> > >         };
> > >
> > >
> > > @Override
> > > @BeforeEach
> > > public void setUp() throws Exception {
> > >     super.setUp();
> > >
> > >     redirectServer = ServerBootstrap.bootstrap()
> > >             .setListenerPort((Integer)parameters.get("port2"))
> > >             .setHttpProcessor(getHttpProcessor())
> > >             .registerHandler("/*", new SimpleHandler("Donald Duck"))
> > >             .create();
> > >     redirectServer.start();
> > >
> > >     localServer = ServerBootstrap.bootstrap()
> > >             .setListenerPort((Integer)parameters.get("port"))
> > >             .setHttpProcessor(getHttpProcessor())
> > >             .registerHandler("/users/*", redirectTo(302,
> > > "/redirected", redirectServer))
> > >             .create();
> > >     localServer.start();
> >
> >
> >
> > --
> > Claus Ibsen
> > -----------------
> > http://davsclaus.com @davsclaus
> > Camel in Action 2: https://www.manning.com/ibsen2
> >



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: Redirect with POST call not possible

Posted by Michael Rambichler <mi...@rambichler.at>.
Hi,

Yes, exactly. I debugged the http-client code already. And the solution is
to define the redirect strategy:
via: client.setRedirectStrategy(new LaxRedirectStrategy());

But i do not know how to access the http-client used by the camel-http

Am Di., 10. Mai 2022 um 16:03 Uhr schrieb Claus Ibsen <claus.ibsen@gmail.com
>:

> Hi
>
> So are you asking for how to configure the http client to follow redirects?
>
> You use camel-http, so you need to find out how it does redirects
>
> On Tue, May 10, 2022 at 3:46 PM Michael Rambichler
> <mi...@rambichler.at> wrote:
> >
> > Hi Team,
> >
> > I hope someone can help us.
> >
> > We have to do a simple rest callout with POST method and http
> > as producercomponent:
> > If we use POST as method, the redirect in the http component is not
> > fulfilled. (If we use GET it works)
> >
> > After some investigation we realized that we have to configure the
> > http-client to use LaxRedirectStrategy
> > But I have no idea how to access the client within camel.
> >
> > Do you have any ideas?
> >
> > Simple TestRoute:
> >
> > protected RouteBuilder createRouteBuilder() throws Exception {
> >         return new RouteBuilder() {
> >             @Override
> >             public void configure() throws Exception {
> >
> >                 restConfiguration()
> >                         .producerComponent("http")
> >                         .host("localhost")
> >                         .port((Integer) parameters.get("port"));
> >
> >                 from("direct:start")
> >
> > .to("rest:POST:users/{id}/basic?throwExceptionOnFailure=false")
> >                         .log(LoggingLevel.INFO, "Received body :
> ${body}")
> >                         .to("mock:result");
> >
> >             }
> >         };
> >
> >
> > @Override
> > @BeforeEach
> > public void setUp() throws Exception {
> >     super.setUp();
> >
> >     redirectServer = ServerBootstrap.bootstrap()
> >             .setListenerPort((Integer)parameters.get("port2"))
> >             .setHttpProcessor(getHttpProcessor())
> >             .registerHandler("/*", new SimpleHandler("Donald Duck"))
> >             .create();
> >     redirectServer.start();
> >
> >     localServer = ServerBootstrap.bootstrap()
> >             .setListenerPort((Integer)parameters.get("port"))
> >             .setHttpProcessor(getHttpProcessor())
> >             .registerHandler("/users/*", redirectTo(302,
> > "/redirected", redirectServer))
> >             .create();
> >     localServer.start();
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
>

Re: Redirect with POST call not possible

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

So are you asking for how to configure the http client to follow redirects?

You use camel-http, so you need to find out how it does redirects

On Tue, May 10, 2022 at 3:46 PM Michael Rambichler
<mi...@rambichler.at> wrote:
>
> Hi Team,
>
> I hope someone can help us.
>
> We have to do a simple rest callout with POST method and http
> as producercomponent:
> If we use POST as method, the redirect in the http component is not
> fulfilled. (If we use GET it works)
>
> After some investigation we realized that we have to configure the
> http-client to use LaxRedirectStrategy
> But I have no idea how to access the client within camel.
>
> Do you have any ideas?
>
> Simple TestRoute:
>
> protected RouteBuilder createRouteBuilder() throws Exception {
>         return new RouteBuilder() {
>             @Override
>             public void configure() throws Exception {
>
>                 restConfiguration()
>                         .producerComponent("http")
>                         .host("localhost")
>                         .port((Integer) parameters.get("port"));
>
>                 from("direct:start")
>
> .to("rest:POST:users/{id}/basic?throwExceptionOnFailure=false")
>                         .log(LoggingLevel.INFO, "Received body : ${body}")
>                         .to("mock:result");
>
>             }
>         };
>
>
> @Override
> @BeforeEach
> public void setUp() throws Exception {
>     super.setUp();
>
>     redirectServer = ServerBootstrap.bootstrap()
>             .setListenerPort((Integer)parameters.get("port2"))
>             .setHttpProcessor(getHttpProcessor())
>             .registerHandler("/*", new SimpleHandler("Donald Duck"))
>             .create();
>     redirectServer.start();
>
>     localServer = ServerBootstrap.bootstrap()
>             .setListenerPort((Integer)parameters.get("port"))
>             .setHttpProcessor(getHttpProcessor())
>             .registerHandler("/users/*", redirectTo(302,
> "/redirected", redirectServer))
>             .create();
>     localServer.start();



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2