Quantcast

How to handle http status code

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

How to handle http status code

bitter geek
Hi All,

I have a router builder for a rest service with configure override
like the following:
...
@Override
        public void configure() throws Exception {
                from("direct:GetCustomer")
                .setHeader("Content-Type", constant("application/x-www-form-urlencoded"))
                .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, constant(Boolean.TRUE))
                .setHeader(Exchange.HTTP_PATH, simple("${header.httpPath}"))
                .setHeader(Exchange.HTTP_QUERY,
                                simple(getURIComponentQuery()))
                .to(endpoint);
        }
...


The rest service returns an xml document. I don't have the xsd for it
so would like to set the type of the response to String via
convertBodyTo(String.class).

But once I do this, producerTemplate.requestBodyAndHeaders(null,
headers) returns a String object. So I lose the status code.

Noting converting it to string will let me access the status code but
then I no longer have the text body available.

Any idea how to get both the status code and the text body properly?

Thanks a lot for the help!

Bing
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to handle http status code

Sergey Beryozkin-3
Hi
On 23/07/12 16:43, bitter geek wrote:

> Hi All,
>
> I have a router builder for a rest service with configure override
> like the following:
> ...
> @Override
> public void configure() throws Exception {
> from("direct:GetCustomer")
> .setHeader("Content-Type", constant("application/x-www-form-urlencoded"))
> .setHeader(Exchange.HTTP_METHOD, constant("GET"))
> .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, constant(Boolean.TRUE))
> .setHeader(Exchange.HTTP_PATH, simple("${header.httpPath}"))
> .setHeader(Exchange.HTTP_QUERY,
> simple(getURIComponentQuery()))
> .to(endpoint);
> }
> ...
>
>
> The rest service returns an xml document. I don't have the xsd for it
> so would like to set the type of the response to String via
> convertBodyTo(String.class).
>
> But once I do this, producerTemplate.requestBodyAndHeaders(null,
> headers) returns a String object. So I lose the status code.
>
> Noting converting it to string will let me access the status code but
> then I no longer have the text body available.
>
> Any idea how to get both the status code and the text body properly?
>
> Thanks a lot for the help!
I can see from the code that a status code is set as
Exchange.HTTP_RESPONSE_CODE header on the out message, so you should be
able to get it from there

Cheers, Sergey

>
> Bing

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to handle http status code

bitter geek
Hi Sergey,

Thanks for the reply.

I'm quite new to Camel. I knew you can get it from the out message but
how do I access the out message from the code snippet I provided?

I'd like to know what is the best way to access both the status and
the response message as a string.

On 7/23/12, Sergey Beryozkin <[hidden email]> wrote:

> Hi
> On 23/07/12 16:43, bitter geek wrote:
>> Hi All,
>>
>> I have a router builder for a rest service with configure override
>> like the following:
>> ...
>> @Override
>> public void configure() throws Exception {
>> from("direct:GetCustomer")
>> .setHeader("Content-Type",
>> constant("application/x-www-form-urlencoded"))
>> .setHeader(Exchange.HTTP_METHOD, constant("GET"))
>> .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
>> constant(Boolean.TRUE))
>> .setHeader(Exchange.HTTP_PATH, simple("${header.httpPath}"))
>> .setHeader(Exchange.HTTP_QUERY,
>> simple(getURIComponentQuery()))
>> .to(endpoint);
>> }
>> ...
>>
>>
>> The rest service returns an xml document. I don't have the xsd for it
>> so would like to set the type of the response to String via
>> convertBodyTo(String.class).
>>
>> But once I do this, producerTemplate.requestBodyAndHeaders(null,
>> headers) returns a String object. So I lose the status code.
>>
>> Noting converting it to string will let me access the status code but
>> then I no longer have the text body available.
>>
>> Any idea how to get both the status code and the text body properly?
>>
>> Thanks a lot for the help!
> I can see from the code that a status code is set as
> Exchange.HTTP_RESPONSE_CODE header on the out message, so you should be
> able to get it from there
>
> Cheers, Sergey
>
>>
>> Bing
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to handle http status code

Christoph Emmersberger
Hi Bing,

simple question, why don't you write e.g. a processor that is configured to your route. Your route could look like thos:

/** Processor defined as spring bean, e.g. via @Named tag */
@Inject
MyHeaderExtractor myHeaderExtractor;

public void configure() throws Exceltion {
        from("direct:GetCustomer")
                .setHeader("direct:GetCustomer")
                // ... continue with your setter to the header
        .process(myHeaderExtractor)
        .to(endpoint);
}

Your header extractor can than implement the processor interface which forces you to implement a process(Exchange exchange) method.

You could also decide simply using the bean processing capabilities of camel as an alternative approach.

@Named
public class MyHeaderExtractor extends Processor {
       
        public void process(Exchange exchange) {
                exchange.getIn().getHeaders(); // returns the header map on the in exchange
                exchange.getIn().getBody(); // returns the body on the in exchange
                // Here you can add some specific header extractions for whatever you need it
                exchange.getOut().getHeaders(); // Would give you the out header
        }
}

Hope this helps, best regards,

Christoph



On Jul 23, 2012, at 7:06 PM, bitter geek wrote:

> Hi Sergey,
>
> Thanks for the reply.
>
> I'm quite new to Camel. I knew you can get it from the out message but
> how do I access the out message from the code snippet I provided?
>
> I'd like to know what is the best way to access both the status and
> the response message as a string.
>
> On 7/23/12, Sergey Beryozkin <[hidden email]> wrote:
>> Hi
>> On 23/07/12 16:43, bitter geek wrote:
>>> Hi All,
>>>
>>> I have a router builder for a rest service with configure override
>>> like the following:
>>> ...
>>> @Override
>>> public void configure() throws Exception {
>>> from("direct:GetCustomer")
>>> .setHeader("Content-Type",
>>> constant("application/x-www-form-urlencoded"))
>>> .setHeader(Exchange.HTTP_METHOD, constant("GET"))
>>> .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
>>> constant(Boolean.TRUE))
>>> .setHeader(Exchange.HTTP_PATH, simple("${header.httpPath}"))
>>> .setHeader(Exchange.HTTP_QUERY,
>>> simple(getURIComponentQuery()))
>>> .to(endpoint);
>>> }
>>> ...
>>>
>>>
>>> The rest service returns an xml document. I don't have the xsd for it
>>> so would like to set the type of the response to String via
>>> convertBodyTo(String.class).
>>>
>>> But once I do this, producerTemplate.requestBodyAndHeaders(null,
>>> headers) returns a String object. So I lose the status code.
>>>
>>> Noting converting it to string will let me access the status code but
>>> then I no longer have the text body available.
>>>
>>> Any idea how to get both the status code and the text body properly?
>>>
>>> Thanks a lot for the help!
>> I can see from the code that a status code is set as
>> Exchange.HTTP_RESPONSE_CODE header on the out message, so you should be
>> able to get it from there
>>
>> Cheers, Sergey
>>
>>>
>>> Bing
>>
>>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to handle http status code

bitter geek
Hi Christoph,

Thanks for your help!

I managed to get it to work based on you code with some changes as shown below.

One change is to call process after to(endpoint). The reponse I need
to handle is that from to(endpoint).
After this change, the getOut() on exchange returns null, so I have to
set the body on in. (why is it null?)

I then create a responseEntity object so the return value from
requestBodyAndHeaders will have both the status and response text
ready to use.

Do you think if this is the right approach to my problem?

Thanks again!
////////////////////////////////////////////////////////////////////
@Override
        public void configure() throws Exception {
                from("direct:GetCustomer")
                .setHeader("Content-Type", constant("application/x-www-form-urlencoded"))
                .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, constant(Boolean.TRUE))
                .setHeader(Exchange.HTTP_PATH, simple("${header.httpPath}"))
                .setHeader(Exchange.HTTP_QUERY,
                                simple(getURIComponentQuery()))
                .to(endpoint)
                .process(new Processor() {
                        @Override
                        public void process(final Exchange exchange) throws Exception {
                                int responseCode =
exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, int.class);
                                String body = exchange.getIn().getBody(String.class);
                                ResponseEntity<String> response = new ResponseEntity<String>(body,
HttpStatus.valueOf(responseCode));
                                exchange.getIn().setBody(response);
                        }
                });
        }
//////////////////////////////////////////////////////
The calling code:

ResponseEntity<String> response = (ResponseEntity<String>)
producerTemplate.requestBodyAndHeaders(null, headers);
                return getDto(response);

///////////////////////////////////////////////////////


On 7/23/12, Christoph Emmersberger <[hidden email]> wrote:

> Hi Bing,
>
> simple question, why don't you write e.g. a processor that is configured to
> your route. Your route could look like thos:
>
> /** Processor defined as spring bean, e.g. via @Named tag */
> @Inject
> MyHeaderExtractor myHeaderExtractor;
>
> public void configure() throws Exceltion {
> from("direct:GetCustomer")
> .setHeader("direct:GetCustomer")
> // ... continue with your setter to the header
> .process(myHeaderExtractor)
> .to(endpoint);
> }
>
> Your header extractor can than implement the processor interface which
> forces you to implement a process(Exchange exchange) method.
>
> You could also decide simply using the bean processing capabilities of camel
> as an alternative approach.
>
> @Named
> public class MyHeaderExtractor extends Processor {
>
> public void process(Exchange exchange) {
> exchange.getIn().getHeaders(); // returns the header map on the in
> exchange
> exchange.getIn().getBody(); // returns the body on the in exchange
> // Here you can add some specific header extractions for whatever you need
> it
> exchange.getOut().getHeaders(); // Would give you the out header
> }
> }
>
> Hope this helps, best regards,
>
> Christoph
>
>
>
> On Jul 23, 2012, at 7:06 PM, bitter geek wrote:
>
>> Hi Sergey,
>>
>> Thanks for the reply.
>>
>> I'm quite new to Camel. I knew you can get it from the out message but
>> how do I access the out message from the code snippet I provided?
>>
>> I'd like to know what is the best way to access both the status and
>> the response message as a string.
>>
>> On 7/23/12, Sergey Beryozkin <[hidden email]> wrote:
>>> Hi
>>> On 23/07/12 16:43, bitter geek wrote:
>>>> Hi All,
>>>>
>>>> I have a router builder for a rest service with configure override
>>>> like the following:
>>>> ...
>>>> @Override
>>>> public void configure() throws Exception {
>>>> from("direct:GetCustomer")
>>>> .setHeader("Content-Type",
>>>> constant("application/x-www-form-urlencoded"))
>>>> .setHeader(Exchange.HTTP_METHOD, constant("GET"))
>>>> .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
>>>> constant(Boolean.TRUE))
>>>> .setHeader(Exchange.HTTP_PATH, simple("${header.httpPath}"))
>>>> .setHeader(Exchange.HTTP_QUERY,
>>>> simple(getURIComponentQuery()))
>>>> .to(endpoint);
>>>> }
>>>> ...
>>>>
>>>>
>>>> The rest service returns an xml document. I don't have the xsd for it
>>>> so would like to set the type of the response to String via
>>>> convertBodyTo(String.class).
>>>>
>>>> But once I do this, producerTemplate.requestBodyAndHeaders(null,
>>>> headers) returns a String object. So I lose the status code.
>>>>
>>>> Noting converting it to string will let me access the status code but
>>>> then I no longer have the text body available.
>>>>
>>>> Any idea how to get both the status code and the text body properly?
>>>>
>>>> Thanks a lot for the help!
>>> I can see from the code that a status code is set as
>>> Exchange.HTTP_RESPONSE_CODE header on the out message, so you should be
>>> able to get it from there
>>>
>>> Cheers, Sergey
>>>
>>>>
>>>> Bing
>>>
>>>
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to handle http status code

Willem.Jiang
Here is some tricks for accessing message header and body.
In the Processor, you can access the message body from InMessage, and You
can also set the  OutMessage body and header. If you doen't set the
OutMessage, Camel will take the InMessage as the InMessage for next
processor, otherwise it will take the OutMessage as the InMessage.
If you set the OutMessage body, you should also copy the header of
InMessage to the OutMessage.

On Tue, Jul 24, 2012 at 3:12 AM, bitter geek <[hidden email]> wrote:

> Hi Christoph,
>
> Thanks for your help!
>
> I managed to get it to work based on you code with some changes as shown
> below.
>
> One change is to call process after to(endpoint). The reponse I need
> to handle is that from to(endpoint).
> After this change, the getOut() on exchange returns null, so I have to
> set the body on in. (why is it null?)
>
> I then create a responseEntity object so the return value from
> requestBodyAndHeaders will have both the status and response text
> ready to use.
>
> Do you think if this is the right approach to my problem?
>
> Thanks again!
> ////////////////////////////////////////////////////////////////////
> @Override
>         public void configure() throws Exception {
>                 from("direct:GetCustomer")
>                 .setHeader("Content-Type",
> constant("application/x-www-form-urlencoded"))
>                 .setHeader(Exchange.HTTP_METHOD, constant("GET"))
>                 .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
> constant(Boolean.TRUE))
>                 .setHeader(Exchange.HTTP_PATH,
> simple("${header.httpPath}"))
>                 .setHeader(Exchange.HTTP_QUERY,
>                                 simple(getURIComponentQuery()))
>                 .to(endpoint)
>                 .process(new Processor() {
>                         @Override
>                         public void process(final Exchange exchange)
> throws Exception {
>                                 int responseCode =
> exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, int.class);
>                                 String body =
> exchange.getIn().getBody(String.class);
>                                 ResponseEntity<String> response = new
> ResponseEntity<String>(body,
> HttpStatus.valueOf(responseCode));
>                                 exchange.getIn().setBody(response);
>                         }
>                 });
>         }
> //////////////////////////////////////////////////////
> The calling code:
>
> ResponseEntity<String> response = (ResponseEntity<String>)
> producerTemplate.requestBodyAndHeaders(null, headers);
>                 return getDto(response);
>
> ///////////////////////////////////////////////////////
>
>
> On 7/23/12, Christoph Emmersberger <[hidden email]> wrote:
> > Hi Bing,
> >
> > simple question, why don't you write e.g. a processor that is configured
> to
> > your route. Your route could look like thos:
> >
> > /** Processor defined as spring bean, e.g. via @Named tag */
> > @Inject
> > MyHeaderExtractor myHeaderExtractor;
> >
> > public void configure() throws Exceltion {
> >       from("direct:GetCustomer")
> >               .setHeader("direct:GetCustomer")
> >               // ... continue with your setter to the header
> >       .process(myHeaderExtractor)
> >       .to(endpoint);
> > }
> >
> > Your header extractor can than implement the processor interface which
> > forces you to implement a process(Exchange exchange) method.
> >
> > You could also decide simply using the bean processing capabilities of
> camel
> > as an alternative approach.
> >
> > @Named
> > public class MyHeaderExtractor extends Processor {
> >
> >       public void process(Exchange exchange) {
> >               exchange.getIn().getHeaders(); // returns the header map
> on the in
> > exchange
> >               exchange.getIn().getBody(); // returns the body on the in
> exchange
> >               // Here you can add some specific header extractions for
> whatever you need
> > it
> >               exchange.getOut().getHeaders(); // Would give you the out
> header
> >       }
> > }
> >
> > Hope this helps, best regards,
> >
> > Christoph
> >
> >
> >
> > On Jul 23, 2012, at 7:06 PM, bitter geek wrote:
> >
> >> Hi Sergey,
> >>
> >> Thanks for the reply.
> >>
> >> I'm quite new to Camel. I knew you can get it from the out message but
> >> how do I access the out message from the code snippet I provided?
> >>
> >> I'd like to know what is the best way to access both the status and
> >> the response message as a string.
> >>
> >> On 7/23/12, Sergey Beryozkin <[hidden email]> wrote:
> >>> Hi
> >>> On 23/07/12 16:43, bitter geek wrote:
> >>>> Hi All,
> >>>>
> >>>> I have a router builder for a rest service with configure override
> >>>> like the following:
> >>>> ...
> >>>> @Override
> >>>>    public void configure() throws Exception {
> >>>>            from("direct:GetCustomer")
> >>>>            .setHeader("Content-Type",
> >>>> constant("application/x-www-form-urlencoded"))
> >>>>            .setHeader(Exchange.HTTP_METHOD, constant("GET"))
> >>>>            .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
> >>>> constant(Boolean.TRUE))
> >>>>            .setHeader(Exchange.HTTP_PATH,
> simple("${header.httpPath}"))
> >>>>            .setHeader(Exchange.HTTP_QUERY,
> >>>>                            simple(getURIComponentQuery()))
> >>>>            .to(endpoint);
> >>>>    }
> >>>> ...
> >>>>
> >>>>
> >>>> The rest service returns an xml document. I don't have the xsd for it
> >>>> so would like to set the type of the response to String via
> >>>> convertBodyTo(String.class).
> >>>>
> >>>> But once I do this, producerTemplate.requestBodyAndHeaders(null,
> >>>> headers) returns a String object. So I lose the status code.
> >>>>
> >>>> Noting converting it to string will let me access the status code but
> >>>> then I no longer have the text body available.
> >>>>
> >>>> Any idea how to get both the status code and the text body properly?
> >>>>
> >>>> Thanks a lot for the help!
> >>> I can see from the code that a status code is set as
> >>> Exchange.HTTP_RESPONSE_CODE header on the out message, so you should be
> >>> able to get it from there
> >>>
> >>> Cheers, Sergey
> >>>
> >>>>
> >>>> Bing
> >>>
> >>>
> >
> >
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to handle http status code

bitter geek
Thanks Willem!

Now I have a different problem.

I need the response message even if it has an http error code. But
Camel seems to throw an exception whenever that happens. And there is
not access to the response body even if I use onException to handle
the exception.

I tried "throwExceptionOnFailure=false" but it does not seem to make
any difference. I still get 404 exception raised and method process
doesn't get called.

Any ideas?

///////////////////////////////////////////////////////////////////
@Override
        public void configure() throws Exception {
                from("direct:GetSubscriptionGsm")
                .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, constant(Boolean.TRUE))
                .setHeader(Exchange.HTTP_QUERY, simple("throwExceptionOnFailure=false")
                .to(endpoint)
                .process(new Processor() {
                        @Override
                        public void process(final Exchange exchange) throws Exception {
                                buildResponse(exchange);
                        }
                });
        }

On 7/24/12, Willem Jiang <[hidden email]> wrote:

> Here is some tricks for accessing message header and body.
> In the Processor, you can access the message body from InMessage, and You
> can also set the  OutMessage body and header. If you doen't set the
> OutMessage, Camel will take the InMessage as the InMessage for next
> processor, otherwise it will take the OutMessage as the InMessage.
> If you set the OutMessage body, you should also copy the header of
> InMessage to the OutMessage.
>
> On Tue, Jul 24, 2012 at 3:12 AM, bitter geek <[hidden email]> wrote:
>
>> Hi Christoph,
>>
>> Thanks for your help!
>>
>> I managed to get it to work based on you code with some changes as shown
>> below.
>>
>> One change is to call process after to(endpoint). The reponse I need
>> to handle is that from to(endpoint).
>> After this change, the getOut() on exchange returns null, so I have to
>> set the body on in. (why is it null?)
>>
>> I then create a responseEntity object so the return value from
>> requestBodyAndHeaders will have both the status and response text
>> ready to use.
>>
>> Do you think if this is the right approach to my problem?
>>
>> Thanks again!
>> ////////////////////////////////////////////////////////////////////
>> @Override
>>         public void configure() throws Exception {
>>                 from("direct:GetCustomer")
>>                 .setHeader("Content-Type",
>> constant("application/x-www-form-urlencoded"))
>>                 .setHeader(Exchange.HTTP_METHOD, constant("GET"))
>>                 .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
>> constant(Boolean.TRUE))
>>                 .setHeader(Exchange.HTTP_PATH,
>> simple("${header.httpPath}"))
>>                 .setHeader(Exchange.HTTP_QUERY,
>>                                 simple(getURIComponentQuery()))
>>                 .to(endpoint)
>>                 .process(new Processor() {
>>                         @Override
>>                         public void process(final Exchange exchange)
>> throws Exception {
>>                                 int responseCode =
>> exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, int.class);
>>                                 String body =
>> exchange.getIn().getBody(String.class);
>>                                 ResponseEntity<String> response = new
>> ResponseEntity<String>(body,
>> HttpStatus.valueOf(responseCode));
>>                                 exchange.getIn().setBody(response);
>>                         }
>>                 });
>>         }
>> //////////////////////////////////////////////////////
>> The calling code:
>>
>> ResponseEntity<String> response = (ResponseEntity<String>)
>> producerTemplate.requestBodyAndHeaders(null, headers);
>>                 return getDto(response);
>>
>> ///////////////////////////////////////////////////////
>>
>>
>> On 7/23/12, Christoph Emmersberger <[hidden email]> wrote:
>> > Hi Bing,
>> >
>> > simple question, why don't you write e.g. a processor that is
>> > configured
>> to
>> > your route. Your route could look like thos:
>> >
>> > /** Processor defined as spring bean, e.g. via @Named tag */
>> > @Inject
>> > MyHeaderExtractor myHeaderExtractor;
>> >
>> > public void configure() throws Exceltion {
>> >       from("direct:GetCustomer")
>> >               .setHeader("direct:GetCustomer")
>> >               // ... continue with your setter to the header
>> >       .process(myHeaderExtractor)
>> >       .to(endpoint);
>> > }
>> >
>> > Your header extractor can than implement the processor interface which
>> > forces you to implement a process(Exchange exchange) method.
>> >
>> > You could also decide simply using the bean processing capabilities of
>> camel
>> > as an alternative approach.
>> >
>> > @Named
>> > public class MyHeaderExtractor extends Processor {
>> >
>> >       public void process(Exchange exchange) {
>> >               exchange.getIn().getHeaders(); // returns the header map
>> on the in
>> > exchange
>> >               exchange.getIn().getBody(); // returns the body on the in
>> exchange
>> >               // Here you can add some specific header extractions for
>> whatever you need
>> > it
>> >               exchange.getOut().getHeaders(); // Would give you the out
>> header
>> >       }
>> > }
>> >
>> > Hope this helps, best regards,
>> >
>> > Christoph
>> >
>> >
>> >
>> > On Jul 23, 2012, at 7:06 PM, bitter geek wrote:
>> >
>> >> Hi Sergey,
>> >>
>> >> Thanks for the reply.
>> >>
>> >> I'm quite new to Camel. I knew you can get it from the out message but
>> >> how do I access the out message from the code snippet I provided?
>> >>
>> >> I'd like to know what is the best way to access both the status and
>> >> the response message as a string.
>> >>
>> >> On 7/23/12, Sergey Beryozkin <[hidden email]> wrote:
>> >>> Hi
>> >>> On 23/07/12 16:43, bitter geek wrote:
>> >>>> Hi All,
>> >>>>
>> >>>> I have a router builder for a rest service with configure override
>> >>>> like the following:
>> >>>> ...
>> >>>> @Override
>> >>>>    public void configure() throws Exception {
>> >>>>            from("direct:GetCustomer")
>> >>>>            .setHeader("Content-Type",
>> >>>> constant("application/x-www-form-urlencoded"))
>> >>>>            .setHeader(Exchange.HTTP_METHOD, constant("GET"))
>> >>>>            .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
>> >>>> constant(Boolean.TRUE))
>> >>>>            .setHeader(Exchange.HTTP_PATH,
>> simple("${header.httpPath}"))
>> >>>>            .setHeader(Exchange.HTTP_QUERY,
>> >>>>                            simple(getURIComponentQuery()))
>> >>>>            .to(endpoint);
>> >>>>    }
>> >>>> ...
>> >>>>
>> >>>>
>> >>>> The rest service returns an xml document. I don't have the xsd for
>> >>>> it
>> >>>> so would like to set the type of the response to String via
>> >>>> convertBodyTo(String.class).
>> >>>>
>> >>>> But once I do this, producerTemplate.requestBodyAndHeaders(null,
>> >>>> headers) returns a String object. So I lose the status code.
>> >>>>
>> >>>> Noting converting it to string will let me access the status code
>> >>>> but
>> >>>> then I no longer have the text body available.
>> >>>>
>> >>>> Any idea how to get both the status code and the text body properly?
>> >>>>
>> >>>> Thanks a lot for the help!
>> >>> I can see from the code that a status code is set as
>> >>> Exchange.HTTP_RESPONSE_CODE header on the out message, so you should
>> >>> be
>> >>> able to get it from there
>> >>>
>> >>> Cheers, Sergey
>> >>>
>> >>>>
>> >>>> Bing
>> >>>
>> >>>
>> >
>> >
>>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to handle http status code

dunnlow
I handle that with a try/catch block.

http://camel.apache.org/try-catch-finally.html

-J



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-handle-http-status-code-tp5716346p5716470.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Loading...