1
|
npm install superagent
|
1
|
var
request = require(
'superagent'
);
|
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
|
import React, { Component } from
'react'
;
import {
AppRegistry,
StyleSheet,
Text,
View
} from
'react-native'
;
var
request = require(
'superagent'
);
//默认应用的容器组件
class App extends Component {
//构造函数
constructor(props) {
super
(props);
this
.state = {
responseText :
null
};
}
//渲染
render() {
return
(
this
.state.responseText}
);
}
//开始请求数据
doRequest(){
var
_that =
this
;
request.get(
'https://httpbin.org/get'
)
.end(
function
(err, res){
_that.setState({responseText: res.text})
console.log(res);
});
}
}
//样式定义
const styles = StyleSheet.create({
container:{
flex: 1,
marginTop:25
},
item:{
margin:15,
height:30,
borderWidth:1,
padding:6,
borderColor:
'#ddd'
,
textAlign:
'center'
},
});
AppRegistry.registerComponent(
'ReactDemo'
, () => App);
|
1
2
3
4
|
request.get(
'https://httpbin.org/get'
)
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
|
request(
'GET'
,
'https://httpbin.org/get'
).end(
function
(err, res){
alert(res.text);
});
|
1
|
request(
'GET'
,
'https://httpbin.org/get'
).then(success, failure);
|
1
2
3
4
|
request.del(
'https://httpbin.org/get'
)
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
|
request(
'https://httpbin.org/get'
,
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
6
|
request.get(
'https://httpbin.org/get'
)
.set(
'API-Key'
,
'foobar'
)
.set(
'Accept'
,
'application/json'
)
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
|
request.get(
'https://httpbin.org/get'
)
.set({
'API-Key'
:
'foobar'
, Accept:
'application/json'
})
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
6
|
request.get(
'https://httpbin.org/get'
)
.query({ query:
'Manny'
})
.query({ range:
'1..5'
})
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
|
request.get(
'https://httpbin.org/get'
)
.query({ query:
'Manny'
, range:
'1..5'
})
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
6
|
request.get(
'https://httpbin.org/get'
)
.query(
'search=Manny'
)
.query(
'range=1..5'
)
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
|
request.get(
'https://httpbin.org/get'
)
.query(
'search=Manny&range=1..5'
)
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
6
|
request.post(
'https://httpbin.org/post'
)
.set(
'Content-Type'
,
'application/json'
)
.send(
'{"name":"tj","pet":"tobi"}'
)
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
|
request.post(
'https://httpbin.org/post'
)
.send({ name:
'tj'
, pet:
'tobi'
})
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
6
|
request.post(
'https://httpbin.org/post'
)
.send({ name:
'tj'
})
.send({ pet:
'tobi'
})
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
6
|
request.post(
'https://httpbin.org/post'
)
.send(
'name=tj'
)
.send(
'pet=tobi'
)
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
6
|
request.post(
'https://httpbin.org/post'
)
.set(
'Content-Type'
,
'application/x-www-form-urlencoded'
)
.send({ name:
'tj'
, pet:
'tobi'
})
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
6
|
request.post(
'https://httpbin.org/post'
)
.type(
'form'
)
.send({ name:
'tj'
, pet:
'tobi'
})
.end(
function
(err, res){
alert(res.text);
});
|
1
2
3
4
5
6
7
|
request.post(
'https://httpbin.org/post'
)
.query({ format:
'json'
})
.query({ dest:
'/login'
})
.send({ name:
'tj'
, pet:
'tobi'
})
.end(
function
(err, res){
alert(res.text);
});
|
1
2
|
request.post(
'https://httpbin.org/get'
)
.set(
'Content-Type'
,
'application/json'
)
|
1
2
|
request.post(
'https://httpbin.org/get'
)
.type(
'Content-Type'
,
'application/json'
)
|
1
2
3
4
5
|
request.post(
'https://httpbin.org/get'
)
.type(
'json'
)
request.post(
'https://httpbin.org/get'
)
.type(
'png'
)
|
1
2
3
4
5
|
request.get(
'https://httpbin.org/get'
)
.retry(2)
.end(
function
(err, res){
alert(res.text);
});
|