Skip to content

Instantly share code, notes, and snippets.

@sdsanchezm
Created July 22, 2023 19:33
Show Gist options
  • Save sdsanchezm/5a08d2d497da010db6a7a88973b0c615 to your computer and use it in GitHub Desktop.
Save sdsanchezm/5a08d2d497da010db6a7a88973b0c615 to your computer and use it in GitHub Desktop.
POST request with headers in React using axios
import React from 'react';
import axios from 'axios';
const ButtonTest = () => {
const handlePostRequest = () => {
const url = 'https://example.com/api/endpointpath';
const data = {
valueA: 'valueA',
valueB: 'valueB',
};
const headers1 = {
'Content-Type': 'application/json',
Authorization: 'Bearer TOKEN_HERE',
};
axios
.post(url, data, { headers1 })
.then(response => {
console.log('Response: ', response.data);
})
.catch(error => {
console.error('Error: ', error);
});
};
return (
<button onClick={handlePostRequest}>Post Requests Here</button>
);
};
export default ButtonTest;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment