1

使用这条骆驼路线向 Google Translate API 发送 POST 请求:

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')

出于某种原因获得 HTTP 400。谁在请求中看到了一些问题?

更新 1 当我使用curl和发送类似的请求时,一切都像一个魅力:

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

4

1 回答 1

1

答案很简单。我只需要明确设置 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')
于 2013-10-12T11:39:40.257 回答