문제

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"

도움이 되었습니까?

해결책

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')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top