2016-01-07 18:25:56 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2016-01-07 18:25:56 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2016-01-07 18:25:56 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2018-05-11 20:32:37 +00:00
|
|
|
var {StyleSheet, Text, TextInput, View, Platform} = ReactNative;
|
2016-01-07 18:25:56 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class XHRExampleFetch extends React.Component<any, any> {
|
2016-02-25 10:44:58 +00:00
|
|
|
responseURL: ?string;
|
2016-09-06 11:16:54 +00:00
|
|
|
responseHeaders: ?Object;
|
2016-01-07 18:25:56 +00:00
|
|
|
|
|
|
|
constructor(props: any) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2018-05-11 20:32:37 +00:00
|
|
|
responseText: null,
|
2016-01-07 18:25:56 +00:00
|
|
|
};
|
|
|
|
this.responseURL = null;
|
2016-09-06 11:16:54 +00:00
|
|
|
this.responseHeaders = null;
|
2016-01-07 18:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
submit(uri: String) {
|
2018-05-11 20:32:37 +00:00
|
|
|
fetch(uri)
|
|
|
|
.then(response => {
|
|
|
|
this.responseURL = response.url;
|
|
|
|
this.responseHeaders = response.headers;
|
|
|
|
return response.text();
|
|
|
|
})
|
|
|
|
.then(body => {
|
|
|
|
this.setState({responseText: body});
|
|
|
|
});
|
2016-01-07 18:25:56 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:16:54 +00:00
|
|
|
_renderHeaders() {
|
|
|
|
if (!this.responseHeaders) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseHeaders = [];
|
|
|
|
var keys = Object.keys(this.responseHeaders.map);
|
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
|
var key = keys[i];
|
|
|
|
var value = this.responseHeaders.get(key);
|
2018-05-11 20:32:37 +00:00
|
|
|
responseHeaders.push(
|
|
|
|
<Text>
|
|
|
|
{key}: {value}
|
|
|
|
</Text>,
|
|
|
|
);
|
2016-09-06 11:16:54 +00:00
|
|
|
}
|
|
|
|
return responseHeaders;
|
|
|
|
}
|
|
|
|
|
2016-01-07 18:25:56 +00:00
|
|
|
render() {
|
|
|
|
var responseURL = this.responseURL ? (
|
|
|
|
<View style={{marginTop: 10}}>
|
|
|
|
<Text style={styles.label}>Server response URL:</Text>
|
|
|
|
<Text>{this.responseURL}</Text>
|
|
|
|
</View>
|
|
|
|
) : null;
|
|
|
|
|
2016-09-06 11:16:54 +00:00
|
|
|
var responseHeaders = this.responseHeaders ? (
|
|
|
|
<View style={{marginTop: 10}}>
|
|
|
|
<Text style={styles.label}>Server response headers:</Text>
|
|
|
|
{this._renderHeaders()}
|
|
|
|
</View>
|
|
|
|
) : null;
|
|
|
|
|
2016-01-07 18:25:56 +00:00
|
|
|
var response = this.state.responseText ? (
|
|
|
|
<View style={{marginTop: 10}}>
|
|
|
|
<Text style={styles.label}>Server response:</Text>
|
|
|
|
<TextInput
|
|
|
|
editable={false}
|
|
|
|
multiline={true}
|
|
|
|
defaultValue={this.state.responseText}
|
|
|
|
style={styles.textOutput}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Text style={styles.label}>Edit URL to submit:</Text>
|
|
|
|
<TextInput
|
|
|
|
returnKeyType="go"
|
|
|
|
defaultValue="http://www.posttestserver.com/post.php"
|
2018-05-11 20:32:37 +00:00
|
|
|
onSubmitEditing={event => {
|
2016-01-07 18:25:56 +00:00
|
|
|
this.submit(event.nativeEvent.text);
|
|
|
|
}}
|
|
|
|
style={styles.textInput}
|
|
|
|
/>
|
|
|
|
{responseURL}
|
2016-09-06 11:16:54 +00:00
|
|
|
{responseHeaders}
|
2016-01-07 18:25:56 +00:00
|
|
|
{response}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
textInput: {
|
|
|
|
flex: 1,
|
|
|
|
borderRadius: 3,
|
|
|
|
borderColor: 'grey',
|
|
|
|
borderWidth: 1,
|
|
|
|
height: Platform.OS === 'android' ? 44 : 30,
|
|
|
|
paddingLeft: 8,
|
|
|
|
},
|
|
|
|
label: {
|
|
|
|
flex: 1,
|
|
|
|
color: '#aaa',
|
|
|
|
fontWeight: '500',
|
|
|
|
height: 20,
|
|
|
|
},
|
|
|
|
textOutput: {
|
|
|
|
flex: 1,
|
|
|
|
fontSize: 17,
|
|
|
|
borderRadius: 3,
|
|
|
|
borderColor: 'grey',
|
|
|
|
borderWidth: 1,
|
|
|
|
height: 200,
|
|
|
|
paddingLeft: 8,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = XHRExampleFetch;
|