The correct way of POSTing data using Fetch to be used by Formidable
See original GitHub issue// React redux saga simplified view
const { loginEmail, loginPwd } = request.payload;
let postLoginSubmitOptions = {
method: "POST",
headers: new Headers({'content-type': 'application/x-www-form-urlencoded'}),
mode: 'no-cors',
body: JSON.stringify({
loginEmail: loginEmail,
loginPwd: loginPwd
})
};
const response = yield call(fetch, `http://www.example.com/register`, postLoginSubmitOptions);
// express simplified view
router.post('/register', function(req, res, next) {
console.log('registering user');
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3333');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
if(err){
console.log(err);
}
console.log(`incoming fields via form parse`);
console.log(fields); //{ '{"loginEmail":"my-email@gmail.com","loginPwd":"my-password"}': '' }
console.log(fields.loginEmail); // undefined
});
I’ve tried different Content-types, setting different Headers manually and also with “new Headers”, using new FormData() then data.append and body: data. Nothing works. I’ve enabled and disabled body-parser in app.js. The best I can do is getting this weird fields - { “string”: “empty_string” }. What am I doing wrong?
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
The correct way of POSTing data using Fetch to be ... - GitHub
React redux saga simplified view const { loginEmail, loginPwd } = request.payload; let postLoginSubmitOptions = { method: "POST", ...
Read more >Using the Fetch API - MDN Web Docs
Supplying request options · Sending a request with credentials included · Uploading JSON data · Uploading a file · Uploading multiple files.
Read more >Formidable Forms API for extendable WordPress forms
Go to 'Settings' → 'Actions & Notifications' and click on 'Send API Data' to add an API action. Insert this in the Notification...
Read more >Uploading Files using Formidable in a Node.js Application
In this article, we will see how to use Formidable to handle multipart/form-data using Node.js, Express, and MongoDB.
Read more >Using JavaScript Fetch with Formidablejs, Expressjs
I'm using Reactjs saga to send a post request using fetch. Also I'm trying to use formidable instead of usual body-parser. I'm getting...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t know where exactly the problem was but tried encoding data differently and then it worked. Getting a nice parsed object now with formidable: { loginEmail: ‘dan@dan.com’, loginPwd: ‘asjdfkjsadlf’ }
Thanks everyone!
Oh yea. And also can try curl POST from terminal or at least https://www.hurl.it, so we can see if it is in the backend (formidable + express server) or on the frontend requesting logic.