Invalid_request_error [gpt-3.5-turbo] with okhttp in java on android studio [RESOLVED]


I followed a tutorial to make an chatgpt app and get this error :

Failed to load response due to {
'error' : {
'message' : 'Invalid URL (POST /chat/v1/completions)',

This is my code :

JSONObject jsonBody = new JSONObject();
        try {
            jsonBody.put("model", "gpt-3.5-turbo");
            jsonBody.put("messages", question);
            jsonBody.put("max_tokens", 4000);
            jsonBody.put("temperature", 0);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        RequestBody body = RequestBody.create(jsonBody.toString(),JSON);
        Request request = new Request.Builder()
                .addHeader("Authorization", "Bearer HIDDEN_KEY")
                .addHeader("Content-Type", "application/json")
        client.newCall(request).enqueue(new Callback() {
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                addResponse("Failed to load response due to pd "+e.getMessage());

            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                    JSONObject jsonObject  = null;
                    try {
                        jsonObject = new JSONObject(response.body().string());
                        JSONArray jsonArray = jsonObject.getJSONArray("choices");
                        String result = jsonArray.getJSONObject(0).getString("message");
                    } catch (JSONException e) {
                        throw new RuntimeException(e);

                    addResponse("Failed to load response due to "+response.body().string());

I tried changing the model, removing the \chat\ in the URL and send the prompt directly in URL too

I’m new to app making and java coding (but I’m no beginner in coding) so I understand that maybe this code isn’t great as I almost only copy and paste the code from the tutorial.

Thanks for your help !

>Solution :

You have a typo. Change this…

…to this.

See the documentation.

Leave a Reply Cancel reply