-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsuperagent.javascript.txt
70 lines (57 loc) · 3.9 KB
/
superagent.javascript.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
┏━━━━━━━━━━━━━━━━┓
┃ SUPERAGENT ┃
┗━━━━━━━━━━━━━━━━┛
ALTERNATIVES ==> #See HTTP client summary
VERSION ==> #0.21.0
#Node module and web script, for AJAX request.
require("superagent") #Usually put in variable REQUEST
#All methods returns REQUEST, so can chain.
REQUEST.METHOD(URL_STR) #"delete" METHOD -> "del"
REQUEST.end
(FUNC(ERROR, RESPONSE)) #
REQUEST.on("error",FUNC(EROR))#Only if ERROR not defined in end(). Parsing ERROR, etc., not 4**|5** error.
REQUEST.timeout(UINT) #Will emit error with ERROR.timeout = UINT after UINT ms for current request + redirects.
REQUEST.redirects(UINT) #Max number of redirects to follow (def: 5)
REQUEST.retry(UINT) ##Retries UINT times if ERROR ECONNRESET, E[SOCKET]TIMEDOUT, EADDRINFO, 502|503|504 or
##REQUEST.timeout()
##Node module superagent-retry (0.3.0), must SUPERAGENT-RETRY(REQUEST)
REQUEST.abort() #
REQUEST.set(OBJ) #Set header. Default: host [C], Accept-Encoding: gzip, deflate [C], Cookie [C],
REQUEST.set(VAR_STR, VAL) #User-agent: "node-superagent/VERSION" [C], Connection: close [C]
REQUEST.unset(VAR_STR) #Removes header
REQUEST.type|accept(STR) #Set Content-Type [C] or Accept [C]
#STR is extension name or MIME type, or "form" for application/x-www-form-urlencoded
REQUEST.withCredentials() #Uses XHR.withCredentials
REQUEST.agent() #Returns a REQUEST2 with cookies enabled.
#Can access cookies at REQUEST2.jar OBJ, with members getCookie(VAR), getCookies(), setCookie()
REQUEST.query(OBJ|STR) #Add query variables
REQUEST.send("VAR=VAL") #Add to request body as Content-Type: application/x-www-form-urlencoded
REQUEST.send(OBJ) #Same for Content-Type: application/json (support nested OBJ)
REQUEST.attach
(VAR[, PATH][, FILENAME]) #Sends multipart/form-data files
REQUEST.field(VAR, VAL) #Sends multipart/form-data fields
REQUEST as IOSTREAM ==> #REQUEST is an IOSTREAM so can:
ISTREAM.pipe(REQUEST) #Pipe ISTREAM to REQUEST request body
REQUEST.pipe(OSTREAM) #Pipe RESPONSE body to OSTREAM
REQUEST.auth(USER, PASSWORD) #HTTP basic authentication. Only for Node. Can also use USER:PASSWORD@ in URL
REQUEST.ca(STR) #Client certificate
RESPONSE
RESPONSE.text #Response as STR. For Node, only if MIME == "text/*", "*/json" or "x-www-form-urlencoded"
RESPONSE.body #Parsed response body for "application/x-www-form-urlencoded" or "application/json", supporting
#nested OBJ). Or also "multipart/form-data" (name="VAR"; filename="VAL" -> VAR=VAL)
RESPONSE.files.VAR #Parsed response body for "multipart/form-data" (check FORMIDABLE) as a FILE
#(similar to MULTIPART). Seems to bug.
RESPONSE.header[VAR_STR] #Header name is lowercase
RESPONSE.type #Like RESPONSE.header["content-type"], but without ;CHARSET=VAL if present, which can be fetched
#at RESPONSE.charset
RESPONSE.status #Status code
RESPONSE.accepted|noContent|
badRequest|unauthorized|
notAcceptable|notFound|
forbidden #True if RESPONSE.status == 202, 204|1223, 400, 401, 406, 404, 403
RESPONSE.statusType #First number of status code
RESPONSE.info|ok|clientError|
serverError|error #True if RESPONSE.statusType == 1, 2, 4, 5
RESPONSE.error #ERROR if RESPONSE.statusType == 4|5
REQUEST.req #
RESPONSE.res #Underlying REQ|RES (Node only)