$_REQUEST gets parameters but not $_POST when using fetch api












0















I am using fetch to send a POST request to the backend 'ajax-admin.php' which then redirects to a php function I have in functions.php.
I decided to replace xmlHttpRequests with 'fetch' api.
I do a post request with fetch like this:



fetch(url, {
method: 'POST',

credentials: "include",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}


})
.then(res => res.json())
.then(data => resolve(data)) // we call resolve with the data
.catch(err => reject(err));

});


Where the query string of the url has the form: action=login&tokend_id=1234



And on the server side I am using $_POST to retrieve the value of the parameters 'action' and 'token_id'.
However what I see is that $_POST is empty while $_REQUEST does have the paremeters. So if I do $_POST['token_id'] I get an error because $_POST is empty. But if I do $_REQUEST['token_id'] , I get the value of that parameter.



Any idea why this is happening?
Why would the parameters be in $_REQUEST but not $_POST.



I checked whether $_POST is empty with this statement:



 if ( isset( $_POST ) && 0 === count($_POST) ) 


Thanks










share|improve this question























  • REQUEST has both post and get, as you see the values in the url, GET is being used not POST

    – user10226920
    Nov 22 '18 at 2:42













  • Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).

    – Funk Forty Niner
    Nov 22 '18 at 2:43


















0















I am using fetch to send a POST request to the backend 'ajax-admin.php' which then redirects to a php function I have in functions.php.
I decided to replace xmlHttpRequests with 'fetch' api.
I do a post request with fetch like this:



fetch(url, {
method: 'POST',

credentials: "include",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}


})
.then(res => res.json())
.then(data => resolve(data)) // we call resolve with the data
.catch(err => reject(err));

});


Where the query string of the url has the form: action=login&tokend_id=1234



And on the server side I am using $_POST to retrieve the value of the parameters 'action' and 'token_id'.
However what I see is that $_POST is empty while $_REQUEST does have the paremeters. So if I do $_POST['token_id'] I get an error because $_POST is empty. But if I do $_REQUEST['token_id'] , I get the value of that parameter.



Any idea why this is happening?
Why would the parameters be in $_REQUEST but not $_POST.



I checked whether $_POST is empty with this statement:



 if ( isset( $_POST ) && 0 === count($_POST) ) 


Thanks










share|improve this question























  • REQUEST has both post and get, as you see the values in the url, GET is being used not POST

    – user10226920
    Nov 22 '18 at 2:42













  • Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).

    – Funk Forty Niner
    Nov 22 '18 at 2:43
















0












0








0








I am using fetch to send a POST request to the backend 'ajax-admin.php' which then redirects to a php function I have in functions.php.
I decided to replace xmlHttpRequests with 'fetch' api.
I do a post request with fetch like this:



fetch(url, {
method: 'POST',

credentials: "include",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}


})
.then(res => res.json())
.then(data => resolve(data)) // we call resolve with the data
.catch(err => reject(err));

});


Where the query string of the url has the form: action=login&tokend_id=1234



And on the server side I am using $_POST to retrieve the value of the parameters 'action' and 'token_id'.
However what I see is that $_POST is empty while $_REQUEST does have the paremeters. So if I do $_POST['token_id'] I get an error because $_POST is empty. But if I do $_REQUEST['token_id'] , I get the value of that parameter.



Any idea why this is happening?
Why would the parameters be in $_REQUEST but not $_POST.



I checked whether $_POST is empty with this statement:



 if ( isset( $_POST ) && 0 === count($_POST) ) 


Thanks










share|improve this question














I am using fetch to send a POST request to the backend 'ajax-admin.php' which then redirects to a php function I have in functions.php.
I decided to replace xmlHttpRequests with 'fetch' api.
I do a post request with fetch like this:



fetch(url, {
method: 'POST',

credentials: "include",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}


})
.then(res => res.json())
.then(data => resolve(data)) // we call resolve with the data
.catch(err => reject(err));

});


Where the query string of the url has the form: action=login&tokend_id=1234



And on the server side I am using $_POST to retrieve the value of the parameters 'action' and 'token_id'.
However what I see is that $_POST is empty while $_REQUEST does have the paremeters. So if I do $_POST['token_id'] I get an error because $_POST is empty. But if I do $_REQUEST['token_id'] , I get the value of that parameter.



Any idea why this is happening?
Why would the parameters be in $_REQUEST but not $_POST.



I checked whether $_POST is empty with this statement:



 if ( isset( $_POST ) && 0 === count($_POST) ) 


Thanks







php ajax post fetch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 2:39









malenamalena

345219




345219













  • REQUEST has both post and get, as you see the values in the url, GET is being used not POST

    – user10226920
    Nov 22 '18 at 2:42













  • Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).

    – Funk Forty Niner
    Nov 22 '18 at 2:43





















  • REQUEST has both post and get, as you see the values in the url, GET is being used not POST

    – user10226920
    Nov 22 '18 at 2:42













  • Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).

    – Funk Forty Niner
    Nov 22 '18 at 2:43



















REQUEST has both post and get, as you see the values in the url, GET is being used not POST

– user10226920
Nov 22 '18 at 2:42







REQUEST has both post and get, as you see the values in the url, GET is being used not POST

– user10226920
Nov 22 '18 at 2:42















Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).

– Funk Forty Niner
Nov 22 '18 at 2:43







Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).

– Funk Forty Niner
Nov 22 '18 at 2:43














1 Answer
1






active

oldest

votes


















2















Where the query string of the url...




What you're actually doing here is sending a POST request to a URL with a query string. The query string is parsed and placed into $_REQUEST and $_GET regardless of the actual HTTP method used. However, $_POST can only be populated with an actual POST request with a request body.



In your fetch() call, specify body: formData. formData can be represented in this querystring format, or in other ways. See also: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body






share|improve this answer
























  • So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.

    – malena
    Nov 23 '18 at 4:33











  • @malena If PHP and your webserver are configured correctly, $_POST will never be populated from the querystring itself. It will only be populated from a parsed POST request body.

    – Brad
    Nov 23 '18 at 5:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423125%2frequest-gets-parameters-but-not-post-when-using-fetch-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2















Where the query string of the url...




What you're actually doing here is sending a POST request to a URL with a query string. The query string is parsed and placed into $_REQUEST and $_GET regardless of the actual HTTP method used. However, $_POST can only be populated with an actual POST request with a request body.



In your fetch() call, specify body: formData. formData can be represented in this querystring format, or in other ways. See also: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body






share|improve this answer
























  • So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.

    – malena
    Nov 23 '18 at 4:33











  • @malena If PHP and your webserver are configured correctly, $_POST will never be populated from the querystring itself. It will only be populated from a parsed POST request body.

    – Brad
    Nov 23 '18 at 5:21
















2















Where the query string of the url...




What you're actually doing here is sending a POST request to a URL with a query string. The query string is parsed and placed into $_REQUEST and $_GET regardless of the actual HTTP method used. However, $_POST can only be populated with an actual POST request with a request body.



In your fetch() call, specify body: formData. formData can be represented in this querystring format, or in other ways. See also: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body






share|improve this answer
























  • So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.

    – malena
    Nov 23 '18 at 4:33











  • @malena If PHP and your webserver are configured correctly, $_POST will never be populated from the querystring itself. It will only be populated from a parsed POST request body.

    – Brad
    Nov 23 '18 at 5:21














2












2








2








Where the query string of the url...




What you're actually doing here is sending a POST request to a URL with a query string. The query string is parsed and placed into $_REQUEST and $_GET regardless of the actual HTTP method used. However, $_POST can only be populated with an actual POST request with a request body.



In your fetch() call, specify body: formData. formData can be represented in this querystring format, or in other ways. See also: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body






share|improve this answer














Where the query string of the url...




What you're actually doing here is sending a POST request to a URL with a query string. The query string is parsed and placed into $_REQUEST and $_GET regardless of the actual HTTP method used. However, $_POST can only be populated with an actual POST request with a request body.



In your fetch() call, specify body: formData. formData can be represented in this querystring format, or in other ways. See also: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 3:21









BradBrad

115k27232393




115k27232393













  • So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.

    – malena
    Nov 23 '18 at 4:33











  • @malena If PHP and your webserver are configured correctly, $_POST will never be populated from the querystring itself. It will only be populated from a parsed POST request body.

    – Brad
    Nov 23 '18 at 5:21



















  • So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.

    – malena
    Nov 23 '18 at 4:33











  • @malena If PHP and your webserver are configured correctly, $_POST will never be populated from the querystring itself. It will only be populated from a parsed POST request body.

    – Brad
    Nov 23 '18 at 5:21

















So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.

– malena
Nov 23 '18 at 4:33





So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.

– malena
Nov 23 '18 at 4:33













@malena If PHP and your webserver are configured correctly, $_POST will never be populated from the querystring itself. It will only be populated from a parsed POST request body.

– Brad
Nov 23 '18 at 5:21





@malena If PHP and your webserver are configured correctly, $_POST will never be populated from the querystring itself. It will only be populated from a parsed POST request body.

– Brad
Nov 23 '18 at 5:21




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423125%2frequest-gets-parameters-but-not-post-when-using-fetch-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]