uri() - nomadnt-iot/uHTTP GitHub Wiki

query()

Description

Get url, part of it or check if it is equals to the given string

Syntax

server.uri();

server.uri(index);

server.uri(uri);

server.uri(index, uri);

Parameters

segment: the index segment of url (see example below) uri: the uri string to compare to current request uri

Returns

const char array containing uri if no uri string argument is passed. Else return true if the passed uri is equals to current request uri or part of it.

Example

#include <SPI.h>
#include <Ethernet.h>
#include <uHTTP.h>

byte macaddr[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
byte ip4addr[4] = {192, 168, 0, 254};

uHTTP server = uHTTP(80);

void setup(){
    Ethernet.begin(macaddr, ip4addr);

    Serial.print(F("Starting uHTTP at "));
    Serial.print(Ethernet.localIP());
    Serial.println(":80");

    server.begin();
}

void loop(){
    EthernetClient response = server.available();
    if(response){

        // Get the complete uri:
        // ie. from "http://192.168.0.254/foo/bar" it will return "foo/bar"
        Serial.print(F("URI: "));
        Serial.println(server.uri());
   
        // Get only first segment from uri:
        // ie. from "http://192.168.0.254/foo/bar" it will return "foo"
        Serial.print(F("Segment[1]: "));
        Serial.println(server.uri(1));

        // Or you can check if entire uri is equals to the given value:
        if(server.uri("/foo/bar")){
            // do something here...
        }

        // Or you can check if part of uri is equals to the given value:
        if(server.uri(2, "bar")){
            // do something here...
        }

        response.println("HTTP/1.1 200 OK");
        response.println("Content-Type: text/plain");
        response.println();
        response.println("Hello World!");
        response.stop();
    }
}
⚠️ **GitHub.com Fallback** ⚠️