-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to make a post request with JSON data in application/x-www-form-urlencoded #263
Comments
You need to serialize it yourself to a string. If you're using jQuery, then you can use However, if your server accepts JSON, it would be much easier to just Anyway, we can't help you with details about communicating with your server. The |
Thanks @mislav Server can't accept JSON data, that is the all problem. I'm trying to solve this with out jquery. Any suggestion? |
I can't solve this problem for you. You could take a look at Zepto's |
You might also consider using |
👍 |
For anyone else who might still have this problem, I've implemented mislav suggestion using lodash here: |
@dgraham using
results in this formData why I try it...
|
A A |
That makes sense. Too bad we need yet another polyfill for that to work.. import URLSearchParams from 'url-search-params';
const searchParams = new URLSearchParams();
for (const prop in params) {
searchParams.set(prop, params[prop]);
}
fetch(url, {
method: 'POST',
body: searchParams
}) And that only works for flat objects.. And than again, when the data is that simple, a manual conversion isn't that hard as well. const searchParams = Object.keys(params).map((key) => {
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
}).join('&');
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: searchParams
}) Thanks for the quick reply and the insight! |
@tiemevanveen Thanks for simple urlencoder :) |
@tiemevanveen Thanks |
How I solved this: I am using "qs" package for stringifying/encoding the json My Request code:
this makes server call as |
@nitinja This solved it for me! Thanks a lot |
enable to get data should get the data in the array of books |
@nitinja |
Where can I get help on |
function GetSomeDeferredStuff() {
var deferreds = [];
for (let count = 1; count <= 10; count++) {
var params = {
html: '<p>Task #'+count+' complete.',
delay: count
}
//Encode the data
const searchParams = Object.keys(params).map((key) => {
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
}).join('&');
//Push fetch requests to an array
deferreds.push(
fetch("/echo/html/",
{
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: searchParams
})
.then(r => r.text())
.then(data => log(data))
);
}
return deferreds;
}
// Log for debugging
function log(html){
console.log(html);
$("div").append(html);
}
// Execute using jQuery or Promise.All() if preferred
$(function() {
$("a").click(function() {
var deferreds = GetSomeDeferredStuff();
$.when.apply($, deferreds).done(function(e,i) {
$("div").append("<p>All done!</p>");
});
});
}); |
@nitinja Thanks a lot |
I have problem with post request authentification while trying to run the code I get error invalide request does anyone know how I could solve this problem, I get this error { error: 'invalid_request' } const dat = {
const URLSearchParams = Object.keys(dat).map((key) => { fetch('https://polarremote.com/v2/oauth2/token', method: 'POST',
}, }).then(function(res) { |
@nitinja I wish I had seen ur post sooner!! been fiddling around with fetch for a week now trying to firgure out if I did the body formatting wrong cos I can't get the API to recognize the body of my request and now finally it's working! YOU'RE A LIFE SAVER! |
How to make a POST request with above JSON data, as content-type
application/x-www-form-urlencoded
using fetch api?The text was updated successfully, but these errors were encountered: