score:3

Accepted answer

To send HTTP requests from React app, you have two main options.

Either use integrated Fetch API (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) or something like axios (https://github.com/axios/axios).

In order to get info from the form, trigger onChange on each input, and save the input state to component state.

onChange = (prop: any, value: string) => {
  this.setState({
    [prop]: value
  });
};

After that, here's an example using fetch API:

const response = fetch("endpoint_url", {
  method: 'POST',
  body: JSON.stringify({
    this.state.name,
    this.state.email
    // Other body stuff
  }),
  headers: {
    'X-Api-Key': API_KEY,
    'Content-Type': 'application/json'
    // Other possible headers
  }
});

And in the end you need to parse the response as JSON using const responseJson = response.json();


Related Query

More Query from same tag