feign icon indicating copy to clipboard operation
feign copied to clipboard

400 returning when calling elasticsearch from feign

Open jaylucas opened this issue 10 years ago • 31 comments

So I have a basic request to a locally running instance of elasticsearch

    @RequestLine("GET /_search?q=body:{body}")
    public ElasticGetResponse getResumes(@Param("body") String body);

Here is what I get through the logger:

[ElasticSearchService#getResumes] ---> GET http://localhost:9200/_search?q=body%3A%7Bbody%7D HTTP/1.1
[ElasticSearchService#getResumes] Content-Length: 21
[ElasticSearchService#getResumes] 
[ElasticSearchService#getResumes] {
  "body" : "phil"
}
[ElasticSearchService#getResumes] ---> END HTTP (21-byte body)
[ElasticSearchService#getResumes] <--- HTTP/1.1 400 Bad Request (154ms)
[ElasticSearchService#getResumes] Content-Length: 470
[ElasticSearchService#getResumes] Content-Type: application/json; charset=UTF-8
[ElasticSearchService#getResumes] 
[ElasticSearchService#getResumes] {"error":{"root_cause":    [{"type":"search_parse_exception","reason":"failed to parse search source. unknown search element [body]","line":2,"col":3}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"1","node":"rOGZzEghRVacdIRh_E9Wog","reason":{"type":"search_parse_exception","reason":"failed to parse search source. unknown search element [body]","line":2,"col":3}}]},"status":400}
[ElasticSearchService#getResumes] <--- END HTTP (470-byte body)

Now of course I dont get the problem when I curl the same endpoint

curl 'http://localhost:9200/_search?q=body:phil'

returns the correct docs

My question is why does feign hate that colon? When I have _search?q=phil it works....

jaylucas avatar Jul 29 '16 20:07 jaylucas

maybe do a curl -v? also, if it doesn't look obvious, you could use wireshark or something to get exactly what went on the wire from feign (ex this would show a Host header value)

codefromthecrypt avatar Jul 29 '16 20:07 codefromthecrypt

So this is what I got with the curl -v

* Connected to localhost (127.0.0.1) port 9200 (#0)
> GET /_search?q=body:phil HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:9200
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Content-Length: 5503 
   * Connection #0 to host localhost left intact
  {"took":4,"timed_out":false,"_shards": .........

I tried using wire shark to view the url but unfortunately I suck at wireshark. I can see the packets (SYN, ACK, FIN) but I can't determine the url past 127.0.0.1 buuuut looking at the logs you can see the destination addr is messed up anyway example:

When I have @RequestLine("GET /_search?q={body}") I get on this line: https://github.com/OpenFeign/feign/blob/master/core/src/main/java/feign/SynchronousMethodHandler.java#L88

SynchronousMethodHandler.java
     Object executeAndDecode(RequestTemplate template) throws Throwable {
             Request request = targetRequest(template); //<------ here my request URL == http://localhost:9200/_search?q=phil

So at this point I already have the correct url.

But with @RequestLine("GET /_search?q=body:{body}") I get at that same line:

  request.url = http://localhost:9200/_search?q=body%3A%7Bbody%7D

So what is happening is that feign is not injecting the the variable (in this case phil) into the @RequestLine("GET /_search?q=body:{body}") seemingly because of the colon?

it turns into %3A%7B which I am assuming is :{ then %7D or }

Any idea?

jaylucas avatar Jul 29 '16 22:07 jaylucas

Any word on this?

jaylucas avatar Aug 03 '16 15:08 jaylucas

sorry.. off radar as in conference right now.

codefromthecrypt avatar Aug 03 '16 18:08 codefromthecrypt

:(

jaylucas avatar Aug 03 '16 20:08 jaylucas

ok looking at your dump, it looks like a request body is being created, eventhough this is a GET request! This sounds very strange to me.

codefromthecrypt avatar Aug 03 '16 21:08 codefromthecrypt

ex the below is showing a json body being made. The code you pasted doesn't seem like it should be creating a json body. Is there anything else (perhaps about how feign is constructed), which might explain this?

[ElasticSearchService#getResumes] ---> GET http://localhost:9200/_search?q=body%3A%7Bbody%7D HTTP/1.1
[ElasticSearchService#getResumes] Content-Length: 21
[ElasticSearchService#getResumes] 
[ElasticSearchService#getResumes] {
  "body" : "phil"
}

codefromthecrypt avatar Aug 03 '16 21:08 codefromthecrypt

This is what I have:

public class ElasticSearchFactory {
   public static ElasticSearchService elasticSearchService() {
        final String host = "http://localhost:9200";

        return Feign.builder()
            .encoder(new JacksonEncoder())
            .decoder(new JacksonDecoder())
            .logger(new Logger.JavaLogger().appendToFile("/logs/http.log"))
            .logLevel(Logger.Level.FULL)
            .target(ElasticSearchService.class, host);
     }

    public interface ElasticSearchService {

       @RequestLine("POST /document/{id}")
        public ElasticPostResponse addInfo(@Param("id") Integer id, Document doc);

       @RequestLine("GET /_search?q={body}")
        public ElasticGetResponse workingGetInfo(@Param("body") String body);

       @RequestLine("GET /_search?q=body:{body}")
       public ElasticGetResponse brokenGetInfo(@Param("body") String body);
   }
}

Creating it in another class.

private ElasticSearchFactory.ElasticSearchService
        elasticSearchService = ElasticSearchFactory.elasticSearchService();

Then calling it like this:

ElasticGetResponse elasticGetResponse = elasticSearchService.getInfo(query);

query being a string

jaylucas avatar Aug 03 '16 22:08 jaylucas

No obvious problem. I will have a look and get back by tomorrow

codefromthecrypt avatar Aug 04 '16 00:08 codefromthecrypt

Thanks for the help!!

jaylucas avatar Aug 04 '16 01:08 jaylucas

This is a bug for sure. Not sure yet which of the various recent changes caused it.

  interface Issue424 {
    @RequestLine("GET /_search?q=body:{body}")
    String get(@Param("body") String body);
  }

  @Test
  public void getDoesntImplyFormParams() throws Exception {
    List<MethodMetadata> mds = contract.parseAndValidatateMetadata(Issue424.class);
    assertThat(mds.get(0).formParams()).isEmpty();
  }

codefromthecrypt avatar Aug 04 '16 18:08 codefromthecrypt

actually, I see what's going on.

We added support for partial-match on headers, and not on queries. Ex. the {body} part is a partial-match on the query param q.

codefromthecrypt avatar Aug 04 '16 18:08 codefromthecrypt

https://github.com/OpenFeign/feign/pull/428

codefromthecrypt avatar Aug 04 '16 18:08 codefromthecrypt

This is great thank you

jaylucas avatar Aug 05 '16 01:08 jaylucas

Sure sorry about the delay

codefromthecrypt avatar Aug 05 '16 04:08 codefromthecrypt

Sorry for the super late response. I was away of vacation.

But I am still getting the same issue

[ElasticSearchService#getResumes] ---> GET http://localhost:9200/_search?q=body%3A%7Bbody%7D HTTP/1.1
[ElasticSearchService#getResumes] Content-Length: 20
[ElasticSearchService#getResumes] 
[ElasticSearchService#getResumes] {
  "body" : "jay"
}
[ElasticSearchService#getResumes] ---> END HTTP (20-byte body)
[ElasticSearchService#getResumes] <--- HTTP/1.1 400 Bad Request (54ms)
[ElasticSearchService#getResumes] content-length: 470
[ElasticSearchService#getResumes] content-type: application/json; charset=UTF-8
[ElasticSearchService#getResumes] 
[ElasticSearchService#getResumes] {"error":{"root_cause":    [{"type":"search_parse_exception","reason":"failed to parse search source. unknown search element [body]","line":2,"col":3}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"1","node":"MWwTeNKxTfGi4QewR6zb9g","reason":{"type":"search_parse_exception","reason":"failed to parse search source. unknown search element [body]","line":2,"col":3}}]},"status":400}
[ElasticSearchService#getResumes] <--- END HTTP (470-byte body)

jaylucas avatar Aug 16 '16 03:08 jaylucas

that doesn't look like almost works :) it looks like "doesn't work at all"!

sure you are on latest feign, right?

codefromthecrypt avatar Aug 16 '16 04:08 codefromthecrypt

Lol I edited my response. I thought It was different then before.

https://mvnrepository.com/artifact/com.netflix.feign/feign-core/8.18.0

I can even go to the line with the edits. So yes.

jaylucas avatar Aug 16 '16 04:08 jaylucas

latest has the fix:

io.github.openfeign feign-core 9.1.0

codefromthecrypt avatar Aug 16 '16 04:08 codefromthecrypt

Yeah I tried this too: https://mvnrepository.com/artifact/io.github.openfeign/feign-core/9.1.0 using gradle so

compile group: 'io.github.openfeign', name: 'feign-core', version: '9.1.0'

jaylucas avatar Aug 16 '16 04:08 jaylucas