ChatGPT API HTTPS requests not working

I keep getting an error 400 from the ChatGPT API when trying to use my Arduino Giga R1 WiFi to send a request
Here's the code

# include <WiFi.h>

char * ssid = "";
char * password = "";
//pretend these are filled in
char * key = "Bearer xxxxxx"; //pretend this is an API key

void setup() {
    Serial.begin(9600);
    if (!Serial) { delay(5000); }
    Serial.print("Connecting to WiFi\n");
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) { continue; }
    Serial.print("Connected!\n");
    WiFiSSLClient client;
    Serial.print("Connecting to OpenAI\n");
    if (!client.connectSSL("api.openai.com", 443)) {
        Serial.print("Connection failed!");
        return;
    }
    Serial.print("Connected!\n");
    String payload = "{\
\"model\": \"gpt-3.5-turbo\",\
\"messages\": [{\
\"role\": \"user\",\
\"content\": \"Why is grass green?\"\
}]\
}";
    client.println("POST /v1/chat/completions HTTPS/1.0");
    client.println("Host: api.openai.com");
    client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    client.println(payload.length());
    client.print("Authorization: ");
    client.println(key);
    client.println();//separate data from headers
    client.println(payload);
    while (client.available()) {
        char c = client.read();
        Serial.print(c);
    }
    if (!client.connected()) {
        Serial.print("disconnected\n");
        client.stop();
    }
}

void loop() {
}

This is the error:
image

This is an example of the web request working as intended (With the same API key & data)

How can I fix this? What's wrong with my formatting?

Only ChatGPT knows.

It does not

My point EXACTLY!!!!!

No, that's the HTTP response code and headers. Any helpful part -- if any -- would be in the body of the response, which got cut off.

Post the entire response as code, not a screenshot.

I would guess it is this

which should instead be

POST /v1/chat/completions HTTP/1.1

I've asked for help in a number of different places and for some reason the most common thing is switching between HTTPS/1.1 and HTTPS/1.0 -- I've tried both, people have told me to use both and none of them have worked sadly

HTTP/1.1

Whether or not the "over the wire" protocol uses TLS or not, the actual request has no S

Consider that HTTPS is often layered on top with a load balancer; the actual request gets to a server in the data center via plain HTTP after that.

2 Likes

changed to "POST /v1/chat/completions HTTP/1.1\r\n" and now it's not even getting a response...The process just hangs

If there is no response available immediately after you print the payload, you go straight to loop, where nothing happens forever. You can verify this by putting a Serial.print there. Try instead

    while (client.connected()) {
        if (client.available()) {
            char c = client.read();
            Serial.print(c);
        }
    }
    Serial.print("disconnected\n");
    client.stop();
}

void loop() {
}

This is not necessarily the most efficient, but less ambiguous

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.