Write vs Println & Bad Requests

Hi guys, I am trying to connect to discord (which I know probably can't happen on Arduino Uno because of the cryptography) and I ran into a problem where I would receive a HTTP 400 response (Bad Request)

It is odd because it happens when I write information to the server instead of println'ing headers.

Example of code that works:

if (client.connect(server, 80)) {
    Serial.println("connecting...");
    client.println("GET /gateway HTTP/1.1");
    client.println("Host: discord.com");
    client.println("Connection: close");
    client.println();
}

Code that is a bad request:

myFile = SD.open("AUP/shake.txt");
while(myFile.available()){
  client.write(buffer, myFile.read(buffer, 80));
}

AUP/shake.txt:

GET /gateway HTTP/1.1\r\nHost: discord.com\r\nConnection: close\r\n\r\n

Unsure why this happens, the code references the text file correctly, I checked with Serial.

Does your textfile contain the text \r\n or the real line break?

if this is text in the file - replace them with real CR and LF in the file.

1 Like

Thanks, it turns out I didn't need to type in the line breaks, I could just indent the text file. Which worked for me.

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