Per this answer,
...
header("Location: ...")
sets a status code that explicitly causes the browser to use a GET request instead of whatever method it was using.
This is exactly the behavior I'm seeing for the app I'm writing that tries to add data to the database via the API.
I want the row with the red line next to it to also say POST.
Here is the logic that calls the API end points that's in a file called makeAPIcalls.php:
# read raw data from request body and stuff it into $_POST
$_POST = json_decode(file_get_contents('php://input'), true);
# set variables using data that was passed in
$id = getParameterValue('id');
// there are others that aren't shown here
# make API call depending on request method
$method = $_SERVER['REQUEST_METHOD'];
$url = 'http://localhost:8080/miscellaneous/APIexamples/CRUD/';
if ($method == 'GET') $url .= 'mobile/list';
if ($method == 'POST') $url .= 'mobile/add';
if ($method == 'PUT') $url .= 'mobile/edit/' . $id;
if ($method == 'DELETE') $url .= 'mobile/remove/' . $id;
header('Location: ' . $url);
Is there an alternative approach to header('Location: '...
for hitting an API end point which keeps the original request method?
Edit: Here is the form and the JS:
<form action="" method="post" id="add">
<legend>Add Mobile</legend>
<ol>
<li>
<label for="add_name">name</label>
<input type="text" name="name" id="add_name"></input>
</li>
<li>
<label for="add_model">model</label>
<input type="text" name="model" id="add_model"></input>
</li>
<li>
<label for="add_color">color</label>
<input type="text" name="color" id="add_color"></input>
</li>
</ol>
<button type="button" onclick="addMobile()">Submit</button>
<button type="reset">Cancel</button>
</form>
function addMobile() {
// get the form data
var formData = {
name: $('#add input[name="name"]').val(),
model: $('#add input[name="model"]').val(),
color: $('#add input[name="color"]').val()
}
// process form
$.ajax({
method:"POST",
url:"js/makeAPIcalls.php",
contentType: 'application/json',
data: JSON.stringify(formData),
async: true,
cache: false
})
.done(function(data){
// log data to console for visibility
// console.log(data);
// clear out form fields
$('#add input[name="name"]').val('');
$('#add input[name="model"]').val('');
$('#add input[name="color"]').val('');
})
.fail(function(data){
console.log(data);
console.log('AJAX call failed');
})
}