#okhttp3 not passing multipart body

1 messages · Page 1 of 1 (latest)

high ridge
#

(My java code is below, ty discord limitations)

I have fast api server, which returns this error (req.body.request and req.body.file are null for some reason)

{"detail":[{"type":"missing","loc":["body","request"],"msg":"Field required","input":null,"url":"https://errors.pydantic.dev/2.5/v/missing"},{"type":"missing","loc":["body","file"],"msg":"Field required","input":null,"url":"https://errors.pydantic.dev/2.5/v/missing"}]}

What im doing wrong? I havt found any good documentation how multipart body in okhttp3 should be made, so i think problem could be there...

(part of api doc if u need)

ruby prairieBOT
#

This post has been reserved for your question.

Hey @high ridge! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

high ridge
#
public static Response makeSendReq(String fileName, String fullPath, String fileField) throws IOException {



    // building json req body
    ObjectMapper objectMapper = new ObjectMapper();
    ObjectNode requestBodyJson = objectMapper.createObjectNode();
    ObjectNode fsEntityJson = objectMapper.createObjectNode();
    fsEntityJson.put("name", fileName);
    fsEntityJson.put("abs_path", fullPath);
    fsEntityJson.put("base_type", "file");
    requestBodyJson.set("fs_entity", fsEntityJson);
    requestBodyJson.put("file_field", fileField);

    String requestBodyJsonString = null;
    try {
      requestBodyJsonString = objectMapper.writeValueAsString(requestBodyJson);
    } catch (JsonProcessingException e) {}
     




    // building request body from json
    final Path _pathToFile = Paths.get(fileName);
    RequestBody fileBody = new RequestBody() {
      @Nullable
      @Override
      public MediaType contentType() {
        return MediaType.parse("application/oclet-stream");
      }

      @Override
      public void writeTo(BufferedSink sink) throws IOException {
        try (Source src = Okio.source(_pathToFile)) {
          System.out.println("idkwhathappens");
          sink.writeAll(src);
        }
      }
    };





    // multipart
    MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", fileName, fileBody);
    MultipartBody.Part requestPart = MultipartBody.Part.createFormData("request", requestBodyJsonString);

    MultipartBody multipartBody = new MultipartBody.Builder()
                                      .addPart(filePart)
                                      .addPart(requestPart)
                                      .build();

#




    // building request
    OkHttpClient client = new OkHttpClient();

    String targetUrl = Config.getPassAppRawUrl() + "/api/fs-service/upload-file";
    Request.Builder req = new Request.Builder()
                              .url(targetUrl)
                              .post(multipartBody);
                              



    // add aCookie
    String aToken = Config.getAToken();
    if (aToken != null) {
      req.addHeader("Cookie", "a-token="+aToken);
    }


    // executing request
    Response response = client.newCall(req.build()).execute();


    
    // returning result
    System.out.println(response.body().string());
    return response;
}
merry belfry
#

i think addPart is for application/x-www-form-urlencoded not multipart/form-data and so okhttp is sending the wrong content type

high ridge
#

ty, worked now

merry belfry
high ridge
#

/close

merry belfry
#

i'll try