Using url parameters in POST request


Using url parameters in POST request
I am having some trouble using url parameters and POST
data together in a single request.
POST
I have a form:
<form method="POST" action="" class="form-inline" _lpchecked="1">
<label for="facetname">Add ethnicity</label>
<input id="facetname" type="text" placeholder="ethnicity name" class="form-control"><button type="submit" class="btn btn-primary">Submit</button>
</form>
This is then posted to my facet
route as follows:
facet
router.post('/:facettype', FacetController.facet_create_post);
The FacetController then tries to access both the facettype
and facetname
elements of the req.params
object but it includes only facettype
:
facettype
facetname
req.params
facettype
exports.facet_create_post = (req,res,next) =>
let facet_type = req.params.facettype;
let name = req.params.facetname;
;
I've also tried req.body
which is blank.
req.body
Have I missed something or does using a URL parameter remove the ability to use POST requests?
'facetname=namehere'
@Li357
req.body
is an empty object for me, although req._body
is true.– bendataclear
36 mins ago
req.body
req._body
1 Answer
1
I found the issue, it was mainly stupidity as I suspected.
I forgot the name
parameter in the form input:
name
<input id="facetname" type="text" placeholder="ethnicity name" class="form-control">
Should have been:
<input id="facetname" name="facetname" type="text" placeholder="ethnicity name" class="form-control">
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
With a form that POSTs, the form data should be in req.body itself, ie req.body is the string
'facetname=namehere'
– Li357
38 mins ago