Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

POST JSON with File not working (multipart/form-data) #118

@will-molloy

Description

@will-molloy

Using these deps: feign-core and feign-gson 12.3. feign-form 3.8.0. JDK 19.


I can't get JSON serialisation working when using POST multipart/form-data for POJO->JSON and File.

Example:

interface Example {
  static Example createInstance() {
    return Feign.builder()
        .encoder(new FormEncoder(new GsonEncoder()))
        .target(Example.class, "https://anything.com");
  }

  @RequestLine("POST")
  @Headers("Content-Type: multipart/form-data")
  void uploadWithDto(@Param("dto") Dto dto, @Param("file") File file);

  record Dto(String word, int number) {}

  public static void main(String[] args) throws URISyntaxException {
    Example api = Example.createInstance();
    File file = new File(Thread.currentThread().getContextClassLoader().getResource("file.txt").toURI());
    api.uploadWithDto(new Dto("hello", 123), file);
  }
}

Debugger showed the POJO is not written to the output at all:
image

If I serialise the POJO first then it works, (but uses text/plain):

interface Example {
  static Example createInstance() {
    return Feign.builder()
        .encoder(new FormEncoder(new GsonEncoder()))
        .target(Example.class, "https://anything.com");
  }

  @RequestLine("POST")
  @Headers("Content-Type: multipart/form-data")
  void uploadWithDto(@Param("dto") String dto, @Param("file") File file);

  record Dto(String word, int number) {}

  public static void main(String[] args) throws URISyntaxException {
    Example api = Example.createInstance();
    File file =
        new File(Thread.currentThread().getContextClassLoader().getResource("file.txt").toURI());
    api.uploadWithDto(new Gson().toJson(new Dto("hello", 123)), file);
  }
}

image


There have been previous issues (#24, #28) on this but I haven't been able to get it to work, what am I missing 😕? Also I'm not using Spring which many answers/solutions seem to use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions