Created
July 22, 2023 19:33
-
-
Save sdsanchezm/5a08d2d497da010db6a7a88973b0c615 to your computer and use it in GitHub Desktop.
POST request with headers in React using axios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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