Skip to content

Parameter Input Reference Example

Parameters in IHttpCore can be written in the following ways:

Built-in Types

In IWebCore, there is a series of built-in types that can be directly referenced. These types are as follows:

IRequest

  • Represents the entire request
  • Writing parameters must be in reference form; otherwise, an error will be reported
  • Can be a non-const reference or a const reference
  • A const reference will restrict the content obtained to only the request information, preventing modifications
  • A non-const reference can obtain more information
  • Examples are as follows:
#pragma once

#include "http/controller/IHttpControllerInterface.h"

class ArgumentTypeController : public IHttpControllerInterface<ArgumentTypeController, true>
{
    Q_GADGET
    $AsController(ArgumentTypeController)
public:
    ArgumentTypeController();

    $GetMapping(emptyRequest)
    QString emptyRequest();

    $GetMapping(irequestRef)
    QString irequestRef(IRequest& request);

    $GetMapping(constIRequestRef)
    QString constIRequestRef(const IRequest& request);

//    $GetMapping(irequest) // this is not valid
    QString irequest(IRequest request);
};

IResponse

  • Represents the response data
  • Can be referenced or not
  • Examples are as follows:
1
2
3
4
5
6
$GetMapping(iresponseRef)
QString iresponseRef(IResponse& response)
{
    response.setHeader("hello", "world");
    return "hello world";
}

IHttpCookieJar

  • This manages cookie information
  • Includes cookies in the request and cookies to be sent to the client
  • Examples are as follows:
    // hello:world, qichu:begining
    $GetMapping(cookieJar)
    QString cookieJar(IHttpCookieJar& jar){
        QString ret;
        auto reqCookieKeys = jar.requestCookieKeys();
        ret.append(QString::fromStdString(reqCookieKeys.join(",")));
        if(jar.getRequestCookies("hello").size() == 0){
            qFatal("error");
        }

        auto reqCookies = jar.requestCookies();
        for(auto val : reqCookies.keys()){
            ret.append(val.toQString()).append(",").append(reqCookies.value(val).toQString());
        }

        auto cookie = jar.getRequestCookie("hello");
        if(cookie.m_value != "world"){
            return "error";
        }

        return ret;
    }
1
2
3
4
5
def test_cookieJar():
    url = serverAddress + "/cookie/cookieJar"
    response = requests.get(url, cookies={"hello":"world", "qichu":"begining"})
    assert response.status_code == 200
    print(response.text)

IHttpHeaderJar

  • This manages headers, including request headers and response headers

  • Examples are as follows:

    $GetMapping(headerJar)
    QString headerJar(IHttpHeaderJar& jar){
        auto headers = jar.requestHeaderKeys();
        if(!jar.containRequestHeaderKey("hello")){
            qFatal("error");
        }

        jar.addResponseHeader("hello", "world");
        if(!jar.containResponseHeaderKey("hello")){
            qFatal("error");
        }
        jar.deleteReponseHeader("hello");
        if(jar.containResponseHeaderKey("hello")){
            qFatal("error");
        }

        auto& header = jar.responseHeaders();
        header.insert("j", "k");
        if(!jar.containResponseHeaderKey("j")){
            qFatal("error");
        }
        if(header.value("j") != "k"){
            qFatal("error");
        }
        header.remove("j");
        if(jar.containResponseHeaderKey("j")){
            qFatal("error");
        }
        if(jar.responseHeaderKeys().contains("j")){
           qFatal("error");
        }

        return "hello world";
    }
def test_headerJar():
    url = serverAddress + "/header/headerJar"
    headers = {
        "hello": "world",
        "abcd" : "efgh"
    }
    response = requests.get(url, headers=headers)
    assert response.status_code == 200
    assert response.headers.get("Server") == "IWebCore"
    assert response.headers.get("Content-Type") == "text/plain; charset=UTF-8"
    assert response.text == "hello world"

IHttpSessionJar

  • This manages session requests and sessions.
  • IWebCore has a built-in simple session implementation. Users can implement their own session.
  • Example is as follows:
$GetMapping(session, /)
QString SessionArgument::session(IHttpSession &session)
{
    session.setValue("hello", "world");
    return "hello world";
}

$GetMapping(sessionValue)
QString SessionArgument::sessionValue(IHttpSession &session)
{
    return session.getValue("hello").toString();
}
def test_session():
    session = requests.Session()
    val = session.get(serverAddress + "/session")
    print(val.cookies)

    val2 = session.get(serverAddress + "/session/sessionValue")
    print(val2.text)
    val.cookies["ISessionId"] == val2.cookies["ISessionId"]
    print(val.cookies["ISessionId"])
    print(val2.cookies["ISessionId"])
    assert val2.text == "world"

IHttpMultiPartJar

  • This manages the collection of multipart requests. It allows you to operate and obtain all the content of multipart requests.
  • Example is as follows:
1
2
3
4
    $PostMapping(multipartjar)
    QString multipartjar(const IHttpMultiPartJar& jar){
        return QString::fromStdString(jar.getNames().join(","));
    }
1
2
3
4
5
6
def test_multipartjar():
    val = requests.post(serverAddress + "/BasicArgument/multipartjar", data={"name": "test"}, files={"file": open("ServerConfig.py", "rb")}, verify= False)
    assert val.status_code == 200
    assert val.text == "name,file"
    print(val.text)
    print(val.status_code)

IHttpCookiePart

  • This queries the content of a specific request cookie
  • Example is as follows:
1
2
3
4
    $GetMapping(cookiePart)
    QString cookiePart(IHttpCookiePart name){
        return name.m_value.toQString();
    }
1
2
3
4
5
6
def test_cookiePart():
    val = requests.get(serverAddress + "/BasicArgument/cookiePart", cookies={"name": "cookie"}, verify=False)
    assert val.status_code == 200
    assert val.text == "cookie"
    print(val.text)
    print(val.status_code)

IHttpMultiPart

  • This queries a specific multipart
  • Example is as follows:
1
2
3
4
5
    $PostMapping(multipart)
    QString multipart(const IHttpMultiPart& name, const IHttpMultiPart& file){
        Q_UNUSED(name)
        return file.m_content.toQString();
    }

```python def test_mulitpart(): val = requests.post(serverAddress + "/BasicArgument/multipart", data={"name": "test"}, files={"file": open("ServerConfig.py", "rb")}, verify= False) assert val.status_code == 200 assert val.text == open("ServerConfig.py").read() print(val.text) print(val.status_code)