如何使用groovy连接到Lucidworks Fusion(solr包装器)API?
我已经开始与Lucidworks(2.1.2)合作,而且我最喜欢使用它。旁注:python“请求”无缝地处理了这个问题,但我很固执,不想使用python Fusion很有前途,我期待在Groovy中使用它 如何最好地使用Groovy(以Groovy的方式)在Fusion中连接到Fusion认证API 我尝试了几种方法(最终发现了一些不可行的方法)。我欢迎反馈为什么basic不适合我,以及其他“简单”的解决方案 以下是我尝试过的:如何使用groovy连接到Lucidworks Fusion(solr包装器)API?,groovy,solr,lucidworks,Groovy,Solr,Lucidworks,我已经开始与Lucidworks(2.1.2)合作,而且我最喜欢使用它。旁注:python“请求”无缝地处理了这个问题,但我很固执,不想使用python Fusion很有前途,我期待在Groovy中使用它 如何最好地使用Groovy(以Groovy的方式)在Fusion中连接到Fusion认证API 我尝试了几种方法(最终发现了一些不可行的方法)。我欢迎反馈为什么basic不适合我,以及其他“简单”的解决方案 以下是我尝试过的: groovyx.net.http.HTTPBuilder hb =
groovyx.net.http.HTTPBuilder hb = new HTTPBuilder(FUSION_API_BASE)
hb.auth.basic(user, pass)
如果401未经授权(我相信是因为编码),那么这将失败。HTTPBuilder来自gradle:
compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
我还尝试:
HttpPost httpPost = new HttpPost(url);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "sean"));
nvps.add(new BasicNameValuePair("password", "mypass"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
还尝试:
String path = '/api/apollo/introspect'
URL url = new URL('http', 'corp', 8764, path)
try {
def foo = url.getContent()
log.info "Foo: $foo"
} catch (IOException ioe){
log.warn "IO ERR: $ioe"
}
抛出(现在预期的)IOError:401。如果有人想知道我失败的更多信息,请告诉我,我可能会用大量的技术细节让你感到厌烦
我无耻地回答我自己的问题(以下),但希望一些优秀的老师能给我一些启发
总而言之:有比我在下面找到的更好/更Groove的解决方案吗?所以我问了这个问题,并发布了我找到的解决方案。希望人们能添加更好的解决方案,甚至解释我在最初的尝试中错过了什么 这是我的首选解决方案(以下三种解决方案都是通过谷歌搜索得到的,但我丢失了链接,请随意戳我一下,我会把它们挖出来——这是原创海报的荣耀): 另一个可行的解决办法是:
RESTClient rest = new RESTClient( 'http://localhost:8764/' )
HttpClient client = rest.client
client.addRequestInterceptor(new HttpRequestInterceptor() {
void process(HttpRequest httpRequest, HttpContext httpContext) {
httpRequest.addHeader('Authorization', 'Basic ' + 'sean:mypass'.bytes.encodeBase64().toString())
}
})
def resp = rest.get( path : path)
assert resp.status == 200 // HTTP response code; 404 means not found, etc.
println resp.getData()
对于那些把分数留在家里的人,有一个python解决方案可供比较:
import requests
from requests.auth import HTTPBasicAuth
rsp = requests.get('http://corp:8764/api/apollo/introspect', auth=HTTPBasicAuth('sean', 'lucid4pass'))
print "Response ok/status code: %s/%s", rsp.ok, rsp.status_code
print "Response content: %s", rsp.content
嗯,
肖恩
RESTClient rest = new RESTClient( 'http://localhost:8764/' )
HttpClient client = rest.client
client.addRequestInterceptor(new HttpRequestInterceptor() {
void process(HttpRequest httpRequest, HttpContext httpContext) {
httpRequest.addHeader('Authorization', 'Basic ' + 'sean:mypass'.bytes.encodeBase64().toString())
}
})
def resp = rest.get( path : path)
assert resp.status == 200 // HTTP response code; 404 means not found, etc.
println resp.getData()
import requests
from requests.auth import HTTPBasicAuth
rsp = requests.get('http://corp:8764/api/apollo/introspect', auth=HTTPBasicAuth('sean', 'lucid4pass'))
print "Response ok/status code: %s/%s", rsp.ok, rsp.status_code
print "Response content: %s", rsp.content