With the code below I can make GET requests to the server and receive the data normally. I assume that for POST use the same type of authentication and requests except selecting the GET or POST method and in the case of POST adding the content type, length and json content to the headers. However, the server is not accepting my POST requests. Could someone please check the code, as I do not have in-depth knowledge of HTTP?
void enviarRequisicao() {
if (client.connect(server, 80)) {
Serial.print(F("Enviando requisição: "));
Serial.println(method + " " + requisition + " HTTP/1.1");
// Linha de requisição
client.println(method + " " + requisition + " HTTP/1.1");
// Cabeçalho do Host
client.println("Host: 192.168.1.217");
// Cabeçalho de Autorização
String ha1 = gerarMD5(String(username) + ":" + realm + ":" + password);
String ha2 = gerarMD5(method + ":" + requisition);
String responseHash = gerarMD5(ha1 + ":" + nonce + ":00000001:" + cnonce + ":auth:" + ha2);
client.println("Authorization: Digest username=\"" + String(username) + "\", "
"realm=\"" + realm + "\", "
"nonce=\"" + nonce + "\", "
"uri=\"" + requisition + "\", "
"qop=auth, "
"nc=00000001, "
"cnonce=\"" + cnonce + "\", "
"response=\"" + responseHash + "\", "
"opaque=\"" + opaque + "\"");
if (method == "POST") {
// Cabeçalho para POST
client.println("Content-Type: application/json");
client.println("Content-Length: " + String(jsonData.length())); // Corrigido
client.println(""); // Linha em branco para separar os cabeçalhos do corpo
client.println(jsonData); // Corpo JSON
}
// Outros cabeçalhos
client.println("Connection: keep-alive");
//client.println("Connection: close"); // Evitando conexões persistentes
client.println("L-Hash: " + lHash);
client.println("P-Hash: " + pHash);
client.println("H-Hash: " + hHash);
client.println("Cookie: " + cookie);
client.println(""); // Finalizando cabeçalhos
Serial.println(F("Requisição enviada!"));
} else {
Serial.println(F("Erro: Falha ao conectar ao servidor"));
}
}
The jsonData is generated by:
void setSysDate() {
// Cria um objeto JSON
StaticJsonDocument<200> jsonDoc;
// Define o valor de "sysDate"
jsonDoc["sysDate"] = "2028/03/23/17/02/28";
// Serializa o JSON para uma string
//String jsonData;
serializeJson(jsonDoc, jsonData);
Serial.print("Serialized Json: ");
Serial.println(jsonData);
// Agora "jsonData" contém o JSON formatado
requisition = "/link/system/set_sys_date";
method = "POST";
// Envia a requisição
enviarRequisicao();
}