Question: how to a POST with form-data correctly
See original GitHub issueHi,
So my question is how I would implement the following (with request
) using isomorphic-fetch
var options = {
url: authUrlRoot + 'auth',
followRedirect: false,
form: {
client_id: config.APP_KEY,
redirect_uri: config.REDIRECT_URL,
response_type: "code"
}
};
request.post(options, function(error, httpResponse, body){
// Do something here..
});
I’ve tried something like
var FormData = require('form-data');
var form = new FormData();
form.append('client_id', config.APP_KEY);
form.append('redirect_url', config.REDIRECT_URL);
form.append('response_type', 'code');
fetch(authUrlRoot + 'auth', { method: 'POST', body: form })
.then(function(response) {
if (response.status >= 400) {
throw new Error("Bad response from server");
}
return response.json();
})
.then(function(data) {
console.log(data);
});
But that gives me “Bad response from server” - status code is 400 Bad request. I might be totally blind, but I cannot seem to figure out what is missing?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:18
- Comments:8
Top Results From Across the Web
Sending form data - Learn web development | MDN
As we'd alluded to above, sending form data is easy, but securing an application can be tricky. Just remember that a front-end developer...
Read more >Formdata not added properly to the post request
I found the answer: Drop the Content-Type in the headers. In this case dropping the headers at all, solved the problem.
Read more >FormData - The Modern JavaScript Tutorial
This chapter is about sending HTML forms: with or without files, with additional fields and so on. FormData objects can help with that....
Read more >Sending Form Data - Questions - n8n community
Simply create everything normally. Then add on the HTTP Request-Node under options “Body Content Type” and select “Form-Data Multipart”. It ...
Read more >How to use fetch to POST form data as JSON to your API
The FormData API is natively supported by most modern browsers and provides a straightforward way of accessing the values for all the fields...
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 FreeTop 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
Top GitHub Comments
The spec says you pass the FormData instance as the body…
You may need additional parameters, depending on your use…
Or you can manually encode your data, example below will take an object… (NOTE: ES6 syntax)
Regarding @tracker1 response I think you should use
URLSearchParams
instead ofFormData
when usingapplication/x-www-form-urlencoded;charset=UTF-8
asContent-Type
header, as indicated in the fetch documentation or the fetch specs