Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react post request

componentDidMount() {
    // Simple POST request with a JSON body using fetch
    const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ title: 'React POST Request Example' })
    };
    fetch('https://jsonplaceholder.typicode.com/posts', requestOptions)
        .then(response => response.json())
        .then(data => this.setState({ postId: data.id }));
}
Comment

how to send a post request with react

  const handler = () => {
        const requestOptions = {
          mode: 'no-cors' as RequestMode,
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ "logo": "t-shirt" })
          };
          fetch('http://localhost:8080/tshirt/19', requestOptions)
              .then(response => response.json())
              .then(data => setApiData({ postId: data.id }));
        }
Comment

How to use Post Method in react

import axios from "axios";
import React from "react";

const baseURL = "https://jsonplaceholder.typicode.com/posts";

export default function App() {
  const [post, setPost] = React.useState(null);

  React.useEffect(() => {
    axios.get(`${baseURL}/1`).then((response) => {
      setPost(response.data);
    });
  }, []);

  function createPost() {
    axios
      .post(baseURL, {
        title: "Hello World!",
        body: "This is a new post."
      })
      .then((response) => {
        setPost(response.data);
      });
  }

  if (!post) return "No post!"

  return (
    <div>
      <h1>{post.title}</h1>
      <p>{post.body}</p>
      <button onClick={createPost}>Create Post</button>
    </div>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to convert char to number in js 
Javascript :: get current url javascript 
Javascript :: js take last item in array 
Javascript :: array notation in javascript 
Javascript :: current datetime js 
Javascript :: javascript get index of object in array 
Javascript :: remove file from input type file jquery 
Javascript :: remove all chars from string and leave only numbers javascript 
Javascript :: js hasownproperty multiple 
Javascript :: xmlhttp js post request 
Javascript :: Error: Node Sass version 5.0.0 is incompatible with ^4.0 
Javascript :: jquery get all file input elements 
Javascript :: javascript generate unique id 
Javascript :: what is JSArray 
Javascript :: autofocus react 
Javascript :: js remove element by tagname 
Javascript :: find word in javascript string 
Javascript :: inline z-index react 
Javascript :: ctx.filltext font size 
Javascript :: first non repeating character javascript 
Javascript :: hello is not defined javascript 
Javascript :: jquery remove closest 
Javascript :: javascript get random item from array 
Javascript :: jquery loop over elements 
Javascript :: input text react 
Javascript :: reactjs download file axios 
Javascript :: angular module with routing cli 
Javascript :: js push params to url 
Javascript :: settings icon in react fontawesome 
Javascript :: js extract options from select 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =