In React Native, a POST API request is used to send data to a server. This is commonly done for login forms, registrations, submitting data, or saving records in a database.
fetch() is the most common methodA POST request uses the HTTP method POST and includes a body containing data. In React Native, this is done using the fetch() API with proper headers and JSON formatting.
// Import required hooks
import React, { useState } from "react";
import { View, Button, Text } from "react-native";
// Function to send POST request
const postData = async () => {
try {
const response = await fetch("https://jsonplaceholder.typicode.com/posts", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
title: "Hello World",
body: "This is a new post",
userId: 1
})
});
const json = await response.json();
console.log(json);
} catch (error) {
console.error(error);
}
};
// Main component
export default function App() {
return (
<View>
<Button title="Send Data" onPress={postData} />
</View>
);
}
Test the POST logic below. This simulates a mobile app sending data to a placeholder API.
? User taps button → ? App sends POST request → ?️ Server processes data → ✅ Response returned
async/await for cleaner code