Quark #

Modules #

Quark v1.2.0 Documentation


Table of Contents

quark/http #

Enum: HttpMethod #

HttpMethod #

  • GET
    GET way
  • POST
    POST way
  • HEAD
    HEAD way
  • DELETE
    DELETE way
  • PUT
    PUT way

Enum: HttpReadyState #

HttpReadyState #

  • Initial
    Initial
  • Ready
    Ready
  • Sending
    Sending
  • Response
    Response
  • Completed
    Completed

StreamResponseCallback #

  • @callback StreamResponseCallback(stream)
  • @param stream: StreamResponse

HttpEvent #

Class: NativeHttpClientRequest #

NativeHttpClientRequest #

nativehttpclientrequest.uploadTotal #

Total amount of data to be uploaded to the server

nativehttpclientrequest.uploadSize #

The data size has been written and uploaded to the server

nativehttpclientrequest.downloadTotal #

Total amount of data to be downloaded

nativehttpclientrequest.downloadSize #

Downloaded data volume

nativehttpclientrequest.readyState #

Request ready state

nativehttpclientrequest.statusCode #

Response status code

nativehttpclientrequest.url #

Request target url

nativehttpclientrequest.httpResponseVersion #

Response http version

  • @get httpResponseVersion: string

nativehttpclientrequest.setMethod(method) #

Setting method way at request

nativehttpclientrequest.setUrl(url) #

Setting request url

nativehttpclientrequest.setSavePath(path) #

Setting local save path for downloading

nativehttpclientrequest.setUsername(user) #

Setting login username

nativehttpclientrequest.setPassword(pwd) #

Setting login password

nativehttpclientrequest.disableCache(disable) #

Disable or enable the cache

nativehttpclientrequest.disableCookie(disable) #

Disable or enable the cookie

nativehttpclientrequest.disableSendCookie(disable) #

Disable or enable the sending of cookie

nativehttpclientrequest.disableSslVerify(disable) #

Disable or enable the SSL verify

nativehttpclientrequest.setKeepAlive(keepAlive) #

Setting whether to keep active

nativehttpclientrequest.setTimeout(timeoutMs) #

Setting request timeout time

nativehttpclientrequest.setRequestHeader(name,value) #

Setting request header k/v

nativehttpclientrequest.setForm(formName,value) #

Setting the request form

nativehttpclientrequest.setUploadFile(formName,localPath) #

Setting the upload file path

nativehttpclientrequest.clearRequestHeader() #

Clear request all of headers

nativehttpclientrequest.clearFormData() #

Clear request all of form data

nativehttpclientrequest.getResponseHeader(headerName) #

Getting response header by the name

nativehttpclientrequest.getAllResponseHeaders() #

Getting all of response headers

nativehttpclientrequest.send(data?) #

Start send the request

nativehttpclientrequest.pause() #

Pause accepat the response data

nativehttpclientrequest.resume() #

Resume accepat the response data

nativehttpclientrequest.abort() #

Abort current request and response

Class: HttpClientRequest #

HttpClientRequest #

For example:

import path from 'quark/path'
import * as fs from 'quark/fs'
var cl = new HttpClientRequest()
cl.setUrl('[`https://www.baidu.com/')`](https://www.baidu.com/'))
cl.setSavePath(path.documents('baidu.html'));
cl.onError.on(function(ev) { console.log(ev.data) })
// Prints: 
// <Buffer 3c 68 74 6d 6c 3e 0d ... >
// <Buffer 3c 21 44 4f 43 54 59 ... > 
// ...
cl.onData.on(function(ev) {
    console.log(ev.data);
})
cl.onEnd.on(function() {
    // Prints:
    // true
    // 4 200
    console.log(fs.existsSync(path.documents('baidu.html')))
    console.log(cl.readyState, cl.statusCode)
})
cl.onReadystateChange.on(function() { console.log(cl.readyState, cl.statusCode) })
cl.send();

var cl2 = new HttpClientRequest()
cl2.setUrl('[`http://192.168.1.100:1026/Tools/uploadFile')`](http://192.168.1.100:1026/Tools/uploadFile'))
cl2.setMethod(HttpMethod.POST);
cl2.setUploadFile('uploadFile', path.resources('util/http.js'))
cl2.onEnd.on(function() {
    // Prints: complete
    console.log('complete')
})
cl2.send();

Interface: RequestOptions #

RequestOptions #

requestoptions.url? #

The target path url to request

requestoptions.method? #

The method way to request

requestoptions.headers? #

setting custom request headers

requestoptions.postData? #

Non post requests ignore this option

requestoptions.save? #

save body content to local disk

requestoptions.upload? #

upload loacl file

requestoptions.timeout? #

request timeout time, default no timeout "0"

requestoptions.disableSslVerify? #

Is disable ssl verify

requestoptions.disableCache? #

Is disable cache

requestoptions.disableCookie? #

Is disable cookie

Interface: ResponseData #

ResponseData #

responsedata.data #

response data

responsedata.httpVersion #

http version

responsedata.statusCode #

status code

responsedata.responseHeaders #

response headers

  • responseHeaders: Dict<string>

request(options) #

Sending HTTP request by the options

For example:

import path from 'quark/path'
// uploat file and save body data
var opts = {
    url: '[`http://192.168.1.100:1026/Tools/uploadFile',`](http://192.168.1.100:1026/Tools/uploadFile',)
    method: HttpMethod.POST,
    headers: { test: 'test' },
    // postData: 'a=A',
    save: path.documents('uploadFile.html'),
    upload: path.resources('util/http.js'),
    disableSslVerify: false,
    disableCache: true,
    disableCookie: false,
};
request(opts).then(function(buff) {
    // Prints: <Buffer ...>
    console.log(buff)
}).catch(e=>{ 
    // Fail
})

requestStream(options,cb) #

Sending HTTP request with stream way by the options

For example:

requestStream({
    url: '[`http://192.168.1.100:1026/'`](http://192.168.1.100:1026/') 
}, (d)=>{
    // Prints: <Buffer ...>
    console.log(d.data) 
}).then(function() {
    console.log('Ok') 
}).catch(err=>{
    //Fail
});

requestSync(options) #

Sending HTTP sync request by the options

For example:

// Prints: <Buffer ...>
try {
    console.log(http.requestSync({ url: '[`http://192.168.1.100:1026/'`](http://192.168.1.100:1026/') }));
} catch(e) {
    //Fail
}

download(url,save) #

Sending HTTP request with get way and save response data

upload(url,localFilePath) #

Uploading local file data with post way

get(url) #

Requesting HTTP data in GET way

getStream(url,cb) #

Requesting HTTP data in GET way and and using the stream way receiving data

post(url,data) #

Sending data to server in POST way

getSync(url) #

Syncing request HTTP data in GET way

postSync(url,data) #

Syncing send data to server by POST

downloadSync(url,save) #

Syncing download data from the url path and save to local path

uploadSync(url,localPath) #

Syncing upload data to server in the POST way

abort(id) #

abort async task by id

userAgent() #

Getting default http user-agent header

setUserAgent(ua) #

Setting default http user-agent header

cachePath() #

Getting HTTP cache saving path

setCachePath(path) #

Setting local HTTP cache saving path

maxConnectPoolSize() #

Getting network connection pool size

setMaxConnectPoolSize(size) #

Setting network connection pool size

clearCache() #

To clear all of caches

clearCookie() #

To clear all of cookies