Question

Using this camel route sending POST request to Google Translate API:

from("direct:start").
setHeader(Exchange.HTTP_METHOD, constant('POST')).
setHeader('X-HTTP-Method-Override', constant('GET')).
setBody(constant('q=Hello')).                                                                                                                                                                                                           
log(LoggingLevel.INFO, 'sourcingtool', '${body}').
to("https://www.googleapis.com/language/translate/v2?key=${api_key}&target=fr").
to('stream:out')

For some reason getting HTTP 400. Who is seeing some problem in the request?

UPDATE 1 When I'm using curl and sending similar request, everyting works like a charm:

curl -XPOST -H "X-HTTP-Method-Override:GET" --data "q=Hello" "https://www.googleapis.com/language/translate/v2?key=MY_API_KEY&target=fr"

Was it helpful?

Solution

The answer was simple. I just needed to explicitly set CONTENT_TYPE:

from("direct:start").
setHeader(Exchange.HTTP_METHOD, constant('POST')).
setHeader(Exchange.CONTENT_TYPE, constant('application/x-www-form-urlencoded')). // this one did a trick
setHeader('X-HTTP-Method-Override', constant('GET')).
setBody(constant('q=Hello')).                                                                                                                                                                                                           
log(LoggingLevel.INFO, 'sourcingtool', '${body}').
to("https://www.googleapis.com/language/translate/v2?key=${api_key}&target=fr").
to('stream:out')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top