Skip to content

Instantly share code, notes, and snippets.

@deivid11
Last active August 29, 2019 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deivid11/d0bc1f70132ec54cc3a8567875f3add9 to your computer and use it in GitHub Desktop.
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
<?php
echo 'Data:'; var_dump($_POST);
echo 'ContentType:'; var_dump($_SERVER["CONTENT_TYPE"]);
echo 'Content:'; var_dump(file_get_contents('php://input'));
const axios = require('axios');
const FormData = require('form-data');
const HOST = 'http://localhost:8000';
const url = 'post.php';
const params = {
name: 'Hello',
email: 'jeje@jfufu.com'
};
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