Skip to content

Commit

Permalink
added the path() string to get only path info
Browse files Browse the repository at this point in the history
  • Loading branch information
hoeken committed Dec 11, 2023
1 parent 13fbceb commit ca19721
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
6 changes: 6 additions & 0 deletions examples/platformio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ void setup()
//api - parameters passed in via query eg. /api/endpoint?foo=bar
server.on("/api", HTTP_GET, [](PsychicRequest *request)
{
//showcase some of the variables
Serial.println(request->host());
Serial.println(request->uri());
Serial.println(request->path());
Serial.println(request->queryString());

//create a response object
StaticJsonDocument<128> output;
output["msg"] = "status";
Expand Down
10 changes: 9 additions & 1 deletion src/PsychicRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,19 @@ const String PsychicRequest::methodStr() {
return String(http_method_str((http_method)this->_req->method));
}

const String PsychicRequest::path() {
int index = _uri.indexOf("?");
if(index == -1)
return _uri;
else
return _uri.substring(0, index);
}

const String& PsychicRequest::uri() {
return this->_uri;
}

const String PsychicRequest::queryString() {
const String& PsychicRequest::query() {
return this->_query;
}

Expand Down
21 changes: 12 additions & 9 deletions src/PsychicRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ class PsychicRequest {
bool hasCookie(const char * key);
const String getCookie(const char * key);

http_method method();
const String methodStr();
const String& uri();
const String& url() { return uri(); }
const String host();
const String contentType();
size_t contentLength();
const String& body();
http_method method(); // returns the HTTP method used as enum value (eg. HTTP_GET)
const String methodStr(); // returns the HTTP method used as a string (eg. "GET")
const String path(); // returns the request path (eg /page?foo=bar returns "/page")
const String& uri(); // returns the full request uri (eg /page?foo=bar)
const String& query(); // returns the request query data (eg /page?foo=bar returns "foo=bar")
const String host(); // returns the requested host (request to http://psychic.local/foo will return "psychic.local")
const String contentType(); // returns the Content-Type header value
size_t contentLength(); // returns the Content-Length header value
const String& body(); // returns the body of the request
const ContentDisposition getContentDisposition();
const String queryString();

const String& queryString() { return query(); } //compatability function. same as query()
const String& url() { return uri(); } //compatability function. same as uri()

void loadParams();
PsychicWebParameter * addParam(PsychicWebParameter *param);
Expand Down

0 comments on commit ca19721

Please sign in to comment.