Issue #2: Certain requests fail with a 400 Bad Request

- writeBytes(String) does not work properly with Unicode characters and may produce invalid UTF-8 - replacing it with an alternative call that converts the JSON string to a proper UTF-8 byte array representation and sends that
This commit is contained in:
Richard Eckart de Castilho 2023-11-12 18:42:34 +01:00
parent 4f70d1ee80
commit 84386cf539

View File

@ -1,8 +1,16 @@
package io.github.amithkoujalgi.ollama4j.core; package io.github.amithkoujalgi.ollama4j.core;
import com.fasterxml.jackson.databind.ObjectMapper; import java.io.BufferedReader;
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException; import java.io.DataOutputStream;
import io.github.amithkoujalgi.ollama4j.core.models.*; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.hc.client5.http.classic.methods.HttpDelete; import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.classic.methods.HttpPost;
@ -16,14 +24,15 @@ import org.apache.hc.core5.http.io.entity.StringEntity;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.BufferedReader; import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.DataOutputStream;
import java.io.IOException; import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
import java.io.InputStreamReader; import io.github.amithkoujalgi.ollama4j.core.models.ListModelsResponse;
import java.net.HttpURLConnection; import io.github.amithkoujalgi.ollama4j.core.models.Model;
import java.net.URL; import io.github.amithkoujalgi.ollama4j.core.models.ModelDetail;
import java.util.List; import io.github.amithkoujalgi.ollama4j.core.models.OllamaAsyncResultCallback;
import java.util.stream.Collectors; import io.github.amithkoujalgi.ollama4j.core.models.OllamaRequestModel;
import io.github.amithkoujalgi.ollama4j.core.models.OllamaResponseModel;
/** /**
* The base Ollama API class. * The base Ollama API class.
@ -242,8 +251,8 @@ public class OllamaAPI {
con.setRequestMethod("POST"); con.setRequestMethod("POST");
con.setDoOutput(true); con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Content-Type", "application/json");
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) { try (OutputStream out = con.getOutputStream()) {
wr.writeBytes(ollamaRequestModel.toString()); out.write(ollamaRequestModel.toString().getBytes(StandardCharsets.UTF_8));
} }
int responseCode = con.getResponseCode(); int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { if (responseCode == HttpURLConnection.HTTP_OK) {