Help with HTTP GET and POST - SOLVED

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();
}

An HTTP request is

  • the request line, starting with the method/verb
  • headers, usually at least the Host
  • a blank line to end the headers
  • the body, if any

So only one blank line (unless the body has one or more blank lines, which JSON usually does not), and the body is last.

If you have further trouble, tell us what the server says in response. It may help.

GET and POST use different methods to transmit data. So you might check if your client supports the post() function, see here

https://forum.arduino.cc/t/how-to-post-json-data-from-nano-33-iot/889339/9

and make the necessary changes....

Good luck!
ec2021

Thanks. The problem was extra blank lines.