Unable to POST, strange response

I am working with a classmate’s code from a project we worked on together last semester. Everything was working fine back then, but now that I come back to it, I’m having issues with our MongDB database.

Here is the particular method where I’m getting hung up:

public String processQuery(String query, String endPoint) {
       try {
           OkHttpClient client = new OkHttpClient().newBuilder().build();
           MediaType mediaType = MediaType.parse("application/json");
           RequestBody body = RequestBody.create(query, mediaType);
           String url = "https://data.mongodb-api.com/app/data-HIDDEN/endpoint/data/v1/action/%s".formatted(endPoint);
           Request request = new Request.Builder()
                   .url(url)
                   .method("POST", body)
                   .addHeader("content-type", "application/json")
                   .addHeader("Access-Control-Request-Headers", "*")
                   .addHeader("api-key", "HIDDEN")
                   .build();
           Response response = client.newCall(request).execute();  // execute the request
           LOGGER.debug("Process Query for Endpoint %s Called for %s %s - \nQuery: \n%s".formatted(endPoint, serverName, serverId, query));

           String tempResponse = Objects.requireNonNull(response.body()).string();
           response.close();
           return tempResponse;
       } catch (IOException e) {
           LOGGER.warn(e.getMessage());
           return defaultEmpty;
       }
}

And here’s what I’m seeing in the console:

10:11:13.451 [Thread-1] DEBUG QueryHandler.class - Process Query for Endpoint insertOne Called for Test Room 939915140835467315 - 
Query: 
{"collection":"Jars",
    "database":"TikoJarTest",
    "dataSource":"PositivityJar",
    "document": {
  "serverID" : "9399514467315",
  "openingCondition" : {
    "hasMessageLimit" : true,
    "messageLimit" : 3,
    "creationDate" : "2022-09-21",
    "openingDate" : "2022-09-21",
    "serverChannelID" : "10211009899320"
  },
  "messages" : [ ]
}}

10:11:13.451 [Thread-1] DEBUG QueryHandler.class - -- Check if Jar Created Post Response --
"Header missing: please add content-type: application/json or application/ejson to specify payload data types"

The project is using okhttp3. I will be happy to provide any other details you may need to know. I have almost zero experience with this, so please bear with me.

Oh, I should note:

  1. In MongoDB, the logs DO show that the database was pinged with a request.

  2. The database remains empty; the POST request is never completed, apparently.

Hi, have you already found a solution? I am facing the same problem.

did you find the solution? I am facing exactly the same problem. Please let me know if you resolved it. I have created a SO question in case you want to answer it there. Thanks

I had this same problem with OkHttp3 as well as Apache HttpClient. The issue was fixed by doing the following:

save the json as bytes with no MediaType

RequestBody body = RequestBody.create(json.getBytes(StandardCharsets.UTF_8 ));

modify the the RequestBuilder

Request request = new Request.Builder().get().url(httpUrl)
                .addHeader("content-type", "application/json") // set content-type.
                .addHeader("api-key", atlasToken) // the Atlas api key.
                .post(body) // Do a POST request with the given contents.
                .build(); // build the Request.

then execute

Response response = client.newCall(request).execute();

The response code is now 201 indicating that an insert was successful.