Coder Social home page Coder Social logo

qsimplerestserver's Introduction

QSimpleRestServer

A very simple Qt REST Server.

It uses QHttpServer from Nikhil Marathe for the HTTP-Server.

QHttpServer uses Joyent's HTTP Parser and is asynchronous and does not require any inheritance.

QSimpleRestServer is available under the MIT License.

Requirements

Requires Qt 5 (maybe Qt 4 works too, not testet jet)

Usage

Include the pri-File into your pro-File

include(REST.pri)

After that you can use the follwing headers to implement your REST-Server.

#include "restserver.h"
#include "restrequestlistener.h"
#include "restrequest.h"

Create a Server and setup some REST - Listener

RESTServer server;
server.addRequestListener( new HelloWorldListener() );
server.listen( 8081 );

The REST - Listener must extent RESTRequestListener. The Qt-Slot System is used to find the right Method.

class HelloWorldListener : public RESTRequestListener
{
  Q_OBJECT
  public:
    explicit HelloWorldListener(QObject * parent = 0);

  signals:

  public slots:
    void http_get_( RESTRequest * request );
};
void HelloWorldListener::http_get_(RESTRequest * request)
{
  request->result()->setData( "Hello World" );
  request->result()->setStatusCode( 200 );
}

The Method "http_get_" will be called for the REST - Resource "/".

Method naming

Every REST - Method has to start with http_ followed by the HTTP-Method ( get, post, put, delete, ... ). After that the resource path is added ( replace all "/" with "_" ).

Example

HTTP-Method: GET REST-Rescouce: /customer/event

Methodname must be: http_get_customer_event

Special namings

You can use the keyword "STAR" als a wildcard in your method names.

A litte example:

  void http_get_customerSTAR(RESTRequest * request);

This method will be handled as Method "GET" for resource customer and all sub resources like /customer/events or /customer/bill.

To get the "path" just call

    request->path()

Working with parameters

All parameters within the URL will be parsed to request->params(). There you get an QMap<QString, QString> with all query parameters.

if( request->params().contains( "echo" ) )
{
  request->result()->setData( request->params()["echo"] );
  request->result()->setStatusCode( 200 );
}
else
{
  request->result()->setData( "echo not set!" );
  request->result()->setStatusCode( 500 );
}

qsimplerestserver's People

Contributors

tropby avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.