# java.io.IOException: Server returned HTTP response code: 400

**URL:** <https://community.openai.com/t/java-io-ioexception-server-returned-http-response-code-400/803968>\
**Category:** API\
**Tags:** api\
**Created:** [June 5, 2024, 7:13pm UTC](https://community.openai.com/t/java-io-ioexception-server-returned-http-response-code-400/803968 "2024-06-05T19:13:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikashmeena7781](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/vikashmeena7781/32/393420_2.png) [@vikashmeena7781](https://community.openai.com/u/vikashmeena7781)\
**Post date:** [June 5, 2024, 7:13pm UTC](https://community.openai.com/t/java-io-ioexception-server-returned-http-response-code-400/803968/1 "2024-06-05T19:13:04Z")

</div>

Please help me with this error, I am stuck at this error since last 3 days { “error”: { “message”: “You didn’t provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR\_KEY), or as the password field (with blank username) if you’re accessing the API from your browser and are prompted for a username and password. You can obtain an API key from [https://platform.openai.com/account/api-keys.](https://platform.openai.com/account/api-keys.)”, “type”: “invalid\_request\_error”, “param”: null, “code”: null } }

Below is relevant code

```java
String base64Image = encodeImage(image_path);
// Create URL object
URI url = new URI(API_URL);
HttpURLConnection connection = (HttpURLConnection)
url.toURL().openConnection();

// Set request method to POST
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
connection.setDoOutput(true);

String jsonInputString = "{\n" +
    " \"model\": \"gpt-4-vision-preview\",\n" +
    " \"max_tokens\": 100,\n" +
    " \"messages\": [\n" +
    " {\n" +
    " \"role\": \"system\",\n" +
    " \"content\": \"" + prompt + "\"\n" +
    " },\n" +
    " {\n" +
    " \"role\": \"user\",\n" +
    " \"content\": [\n" +
    " {\"type\": \"text\", \"text\": \"" + query + "\"},\n" +
    " {\"type\": \"image_url\", \"image_url\": {\"url\": \"data:image/jpeg;base64," + base64Image + "\"}}\n" +
    " ]\n" +
    " }\n" +
    " ],\n" +
    " \"temperature\": 0.4\n" +
    "}";

```

I am sure that am using the right api\_key and api\_url . Any lead or help would be highly appreciable.  
Thank you

---

<div class="post-metadata">

**Author:** ![PaulBellow](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/paulbellow/32/597962_2.png) [@PaulBellow](https://community.openai.com/u/PaulBellow)\
**Post date:** [June 5, 2024, 9:10pm UTC](https://community.openai.com/t/java-io-ioexception-server-returned-http-response-code-400/803968/2 "2024-06-05T21:10:36Z")

</div>

Welcome to the dev forum.

Can you use cURL or something to check and make sure the key is valid?

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [June 6, 2024, 2:06am UTC](https://community.openai.com/t/java-io-ioexception-server-returned-http-response-code-400/803968/3 "2024-06-06T02:06:10Z")

</div>

The API acts like you are not sending it a value for the API key.

See if that is true:

```auto
URI url = new URI(API_URL);
HttpURLConnection connection = (HttpURLConnection)
url.toURL().openConnection();

// Validate API_KEY
if (API_KEY == null || API_KEY.isEmpty() || !API_KEY.startsWith("sk-")) {
    System.out.println("Invalid API_KEY: " + API_KEY);
    throw new IllegalArgumentException("API_KEY is invalid or not set correctly.");
}

// Set request method to POST
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
connection.setDoOutput(true);

// Send the request
..

```

Then I’m not sure, but ponder the result if you move openConnection(); to **after** setting the connection method and properties…

---

<div class="post-metadata">

**Author:** ![vikashmeena7781](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/vikashmeena7781/32/393420_2.png) [@vikashmeena7781](https://community.openai.com/u/vikashmeena7781)\
**Post date:** [June 6, 2024, 5:28am UTC](https://community.openai.com/t/java-io-ioexception-server-returned-http-response-code-400/803968/4 "2024-06-06T05:28:06Z")

</div>

Thank you for your time , I have included the validate\_api\_key part code , but it is still showing the same error. However the below code works perfectly, I think the issue is with image part , Since the length of base64image is 354848 , so can it be token exceed issue (though it does not show any error like that) ?  
String jsonInputString2 = “{\n” +  
" "model": "gpt-4-vision-preview",\n" +  
" "messages": [\n" +  
" {\n" +  
" "role": "system",\n" +  
" "content": "You are a helpful assistant."\n" +  
" },\n" +  
" {\n" +  
" "role": "user",\n" +  
" "content": "“+command+”"\n" +  
" }\n" +  
" ]\n" +  
“}”;

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [June 6, 2024, 5:41am UTC](https://community.openai.com/t/java-io-ioexception-server-returned-http-response-code-400/803968/5 "2024-06-06T05:41:58Z")

</div>

While the binary file you send may be large, as long as you use the proper “content” message format to send it, it will only count for between 85 - 1445 tokens depending on the quality and size you specify.

If you made a mistake and sent garbage text that was too long, the API would return a different error, that you exceeded the model’s context length or your usage limit, both with the message having a token count.

Here’s a link to a post showing how the body of a message should be sent for base64.

> [@400 Error when I include base64 image in a prompt](https://community.openai.com/t/400-error-when-i-include-base64-image-in-a-prompt/788596/2):
>
> You don’t modify the contents of a base64 unless you want to corrupt it. This is the format of message that must be sent to chat completions: [{ "role": "system", "content": [{"type": "text", "text": system\_prompt}] }, { "role": "user", "content": [ {"type": "text", "text": "Describe this art style."}, { "type": "image\_url", "image\_url": {"url": f"data:image/jpeg;base64,{e…

I’ll let you decipher what your code is doing between all the escaping.

---

<div class="post-metadata">

**Author:** ![vikashmeena7781](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/vikashmeena7781/32/393420_2.png) [@vikashmeena7781](https://community.openai.com/u/vikashmeena7781)\
**Post date:** [June 6, 2024, 3:47pm UTC](https://community.openai.com/t/java-io-ioexception-server-returned-http-response-code-400/803968/6 "2024-06-06T15:47:58Z")

</div>

I just changes the way that I was passing the string , I created a json object and then convert it to string.  
Thank you for your time everyone
