Last active
August 29, 2019 21:25
-
-
Save deivid11/d0bc1f70132ec54cc3a8567875f3add9 to your computer and use it in GitHub Desktop.
Send a correctly formed multipart/form-data request to a php script using node and 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
<?php | |
echo 'Data:'; var_dump($_POST); | |
echo 'ContentType:'; var_dump($_SERVER["CONTENT_TYPE"]); | |
echo 'Content:'; var_dump(file_get_contents('php://input')); |
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
const axios = require('axios'); | |
const FormData = require('form-data'); | |
const HOST = 'http://localhost:8000'; | |
const url = 'post.php'; | |
const params = { | |
name: 'Hello', | |
email: '[email protected]' | |
}; | |
let data = new FormData(); | |
data.append('data', JSON.stringify(params)); | |
data.append('_method', 'post'); | |
return axios({ | |
baseURL: HOST, | |
method: 'post', | |
headers:{ | |
"Content-Type": 'multipart/form-data; boundary='+data.getBoundary() | |
}, | |
url, | |
data: data.getBuffer() | |
}).then( | |
r=>{ | |
console.log(r.data); | |
console.log('success') | |
} | |
).catch( | |
e=>{ | |
console.log('error!') | |
console.log(e.response.status); | |
console.log(e.response.statusText) | |
} | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment