axios instance request with ssl and non ssl
npm install react-native-get-post-requestThe 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.
- 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.
You can install this package via npm:
``bash`
npm i react-native-get-post-request
1. Create the certificates:
- Run the following command to obtain a certificate:
`bash`
openssl s_client -showcerts -servername google.com -connect google.com:443 nano mycert.pem
- Copy the certificate (usually the first one in the chain), and save it using an editor like ..cer
- Convert it to format:`
bash`
openssl x509 -in mycert.pem -outform der -out mycert.cer
- More details on obtaining certificates.
1. Drag the .cer certificate to your Xcode project, mark your target, and select "Copy items if needed".
2. No extra step is needed for public key pinning, as AFNetworking will extract the public key from the certificate.
1. If using certificate pinning, place your .cer files under src/main/assets/.`
2. For public key pinning, use the following command to extract the public key:
bash`
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
Example: Usage with usePostDataToServer
`javascript
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 (
{isLoading && (
)}
);
}
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;
`
Example: Usage with useGetDataFromServer
`javascript
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 (
{isLoading && (
)}
);
}
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;
``
| 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. |
- data: The fetched or posted data.
- isLoading: Indicates if the request is in progress.
- isError: Indicates if an error occurred.
This project is licensed under the ISC License.