GET request to external REST API

  • I am trying to write a script so the Shelly is sending a REST request to a external server.

    The info I need is:
    how to add a header to the request using Shelly.call() ?

    I am getting response.code 401, it seems that the header with the auth info is not send correctly.

    My script so far :

    Edit by 66er:

    Code placed in Code-Tag

    Einmal editiert, zuletzt von tom321123 (27. Juli 2023 um 21:15)

  • 401, Unauthorized,

    Make sure you are using a valid API key, and if Shelly has set up a PW, try removing it temporarily.

    HTTP.get is not the right thing for this, try HTTP.Request.

    HTTP | Shelly Technical Documentation
    The HTTP service provides functionality for sending HTTP/HTTPS requests through the RPC methods HTTP.GET and HTTP.POST. Also a generic HTTP.Request RPC call is…
    shelly-api-docs.shelly.cloud

    Also keep in mind,

    NOTE

    The HTTP server cannot process requests of total size greater than 3072 bytes. This includes the HTTP request line, headers and body. In this case connection is reset and no response is sent.

    NOTE

    Content-Length and Connection: close headers are automatically set in the response and override any such headers set by the script. Content-Type header defaults to text/plain if not set by the script.

  • For example you can try that

    Shelly.call("http.get", {url: "https://www.api-couleur-tempo.fr/api/jourTempo/today",timeout: 1,ssl_ca: "*"}, RpcBackLectureTempoJourJ);


    The json answer: {"dateJour": "2023-12-01","codeJour": 0,"periode": "2023-2024"}

    The function to read json answer

    function RpcBackLectureTempoJourJ(resultat, error_code, error_message, userdata)

    {

    if (error_code === 0) //--- pas d'erreur de lecture

    {

    if (resultat)

    {

    if (resultat.body !== undefined)

    {

    // print(resultat.body);

    let json = JSON.parse(resultat.body);

    if (json )

    {

    CodeCouleurJourJ = json.codeJour;

    if (CodeCouleurJourJ===0) CouleurJourJ="Inconnu";

    else if (CodeCouleurJourJ===1) CouleurJourJ="BLEU";

    else if (CodeCouleurJourJ===2) CouleurJourJ="BLANC";

    else if (CodeCouleurJourJ===3) CouleurJourJ="ROUGE";

    }

    }

    }

    }

    }

    everything works correctly

  • Dieses Thema enthält einen weiteren Beitrag, der nur für registrierte Benutzer sichtbar ist, bitte registrieren Sie sich oder melden Sie sich an um diesen lesen zu können.