react-native-get-post-request
v1.1.2
Published
axios instance request with ssl and non ssl
Downloads
713
Maintainers
Readme
react-native-get-post-request
Description
The react-native-get-post-request package simplifies fetching and posting data in React Native applications. It provides flexible, secure API requests, loading state management, and error handling, while also supporting certificate-based security for both GET and POST requests.
Features
- Flexible API Requests: Fetch and post data with optional headers, payloads, and certificates.
- Loading State Management: Automatically manages loading states for both GET and POST requests.
- Error Handling: Handle errors efficiently during API interactions.
- Data Transformation: Customize how the data is processed upon fetching or posting.
- Secure API Requests: Supports optional certificate validation for secure connections.
- Activity Indicators: Provides feedback while loading, with customizable indicators.
Installation
You can install this package via npm:
npm i react-native-get-post-request
Integration Process
- Create the certificates:
- Run the following command to obtain a certificate:
openssl s_client -showcerts -servername google.com -connect google.com:443 </dev/null
- Copy the certificate (usually the first one in the chain), and save it using an editor like
nano mycert.pem
. - Convert it to
.cer
format:openssl x509 -in mycert.pem -outform der -out mycert.cer
- More details on obtaining certificates.
- Run the following command to obtain a certificate:
iOS Integration:
- Drag the
.cer
certificate to your Xcode project, mark your target, and select "Copy items if needed". - No extra step is needed for public key pinning, as AFNetworking will extract the public key from the certificate.
Android Integration:
- If using certificate pinning, place your
.cer
files undersrc/main/assets/
. - For public key pinning, use the following command to extract the public key:
openssl s_client -servername google.com -connect google.com:443 | openssl x509 -pubkey -noout | openssl rsa -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64
Usage
import React from 'react';
import { ActivityIndicator, Platform, StyleSheet, Text, View } from 'react-native';
import { usePostDataToServer } from 'react-native-get-post-request';
const certificateObject = { live_certs: [...] };
function App() {
const { data, isLoading, isError, postData } = usePostDataToServer();
const handlePostFetch = () => {
postData({
API_URL: 'url',
structureApiData: (data) => data,
onPostReqSuccess: (finalData) => console.log('Success:', finalData),
onError: (error) => console.error('Error:', error),
secure: true,
certificate: certificateObject,
});
};
return (
<View style={styles.container}>
<Text style={styles.buttonColor} onPress={handlePostFetch}>Button</Text>
{isLoading && (
<View style={styles.loadingOverlay}>
<ActivityIndicator color={'white'} size={Platform.OS === 'android' ? 100 : 'large'} />
</View>
)}
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'lightblue' },
buttonColor: { fontSize: 30, backgroundColor: 'red', padding: 20, color: 'white' },
loadingOverlay: { position: 'absolute', height: 1000, width: 1000, justifyContent: 'center', backgroundColor: 'black', opacity: 0.5 },
});
export default App;
import React from 'react';
import { ActivityIndicator, Platform, StyleSheet, Text, View } from 'react-native';
import { useGetDataFromServer } from 'react-native-get-post-request';
const certificateObject = { live_certs: [...] };
function App() {
const { data, isLoading, isError, fetchData } = useGetDataFromServer();
const handlePostFetch = () => {
fetchData({
API_URL: 'url',
structureApiData: (data) => data,
onGetReqSuccess: (finalData) => console.log('Success:', finalData),
onError: (error) => console.error('Error:', error),
secure: true,
certificate: certificateObject,
});
};
return (
<View style={styles.container}>
<Text style={styles.buttonColor} onPress={handlePostFetch}>Button</Text>
{isLoading && (
<View style={styles.loadingOverlay}>
<ActivityIndicator color={'white'} size={Platform.OS === 'android' ? 100 : 'large'} />
</View>
)}
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'lightblue' },
buttonColor: { fontSize: 30, backgroundColor: 'red', padding: 20, color: 'white' },
loadingOverlay: { position: 'absolute', height: 1000, width: 1000, justifyContent: 'center', backgroundColor: 'black', opacity: 0.5 },
});
export default App;
Hook Parameters (GET and POST)
| Parameter | Type | Required | Description | | ---------------- | -------- | --------------- | ------------------------------------------------------ | | API_URL | string | Yes | The URL of the API endpoint. | | HEADERS | object | No | Custom headers for the API request (default: {}). | | body | object | No | Data to be sent with POST requests (optional). | | secure | boolean | No | Enforce certificate-based security (default is false). | | certificate | object | Yes (if secure) | A certificate object for SSL validation. | | onPostReqSuccess | function | Yes (POST) | Callback for successful POST requests. | | onGetReqSuccess | function | Yes (GET) | Callback for successful GET requests. | | onError | function | Yes | Callback for handling errors during the request. |
Return Values (GET and POST)
- data: The fetched or posted data.
- isLoading: Indicates if the request is in progress.
- isError: Indicates if an error occurred.
License
This project is licensed under the ISC License.