GithubHelp home page GithubHelp logo

mohammadhasanzadeh / quperagent Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 1.0 31 KB

Superagent like HTTP client for the QtQuick/QML

License: MIT License

JavaScript 80.27% QMake 1.97% C++ 0.53% QML 17.24%
http-client qml superagent qt qtquick duperagent

quperagent's Introduction

quperagent

Superagent like HTTP client for QML

Add to project:

Add quperagent.pri to your .pro file:

include(path/to/quperagent.pri)

And then import quperagent.js into your qml file:

import "qrc:/quperagent.js" as HTTPC

Usage:

HTTP requests:

a simple quperagent request can be look like the following:

        HTTPC.request()
        .get(`https://example.test`)
        .end((res) => {}, (err) => {});

end() function gets two callbacks to deliver success response or failure response and also quperagent will serialize response if possible. Each success response and failure response objects will have the following properties:

  • status => HTTP response status
  • text => HTTP raw response
  • body => Serialized response, if content-type header of response equal to application/json

What about the other HTTP methods:

        HTTPC.request()
        .post(`https://example.test`)

        HTTPC.request()
        .put(`https://example.test`)

        HTTPC.request()
        .del(`https://example.test`)

Add headers to request:

You can use the set() method for adding headers to the request like the following, note that, quperagent will add application/json to the content-type if not any header set to the request:

        HTTPC.request()
        .post(`https://example.test`)
        .set({
                 "Content-type": "application/json",
                 "Authorization": `Basic `
             })
        .end((res) => {}, (err) => {});

Add query string to the request:

You can add a query string to the some HTTP request like GET and DELETE easily with the query() method:

        HTTPC.request()
        .get(`https://example.test`)
        .query({
                   "search": "foo",
                   "sort": "bar",
                   "groupby": ["foo", "bar"]
                })
        .end((res) => {}, (err) => {});

Also, you may need to send a raw query string in some cases, so you can pass a simple string to the query():

        HTTPC.request()
        .get(`https://example.test`)
        .query("search=foo&sort=bar&groupby=foo&groupby=bar")
        .end((res) => {}, (err) => {});

Send HTTP data on body:

You can send payload on request body by passing a javascript object to the send() method:

        HTTPC.request()
        .post(`https://example.test`)
        .send({
                   "search": "foo",
                   "sort": "bar"
                })
        .end((res) => {}, (err) => {});

ququperagent will serialized payload based on the content-type header automatically:

  • if content-type set to the application/json then quperagent will send body as a JSON string
  • if content-type set to the application/x-www-form-urlencoded then quperagent will send body as an url encoded data
  • else, quperagent will send payload as a raw data

quperagent's People

Contributors

mohammadhasanzadeh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

lzhice

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.