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.