1

This question title was edited. The original title was "How can I submit a string in a form and have it applied as a request parameter in the url?".


Think of a simple server where a string is received as a route parameter, e.g. with localhost:3000/hello it serves a page containing 'hello'.

This is easily implemented, here is a simple express server doing that:

var express = require("express");
var app = express();

app.get("/:string", function(req, res) {
  res.send(req.params.string);
});

app.listen(3000);

Is it possible to supply the string via a form?

stefaleon
  • 81
  • 1
  • 10

2 Answers2

0

Isn't that what hidden parameters are for to start with...?

<form action="http://www.example.com" method="GET">
  <input type="hidden" name="a" value="1" /> 
  <input type="hidden" name="b" value="2" /> 
  <input type="hidden" name="c" value="3" /> 
  <input type="submit" /> 
</form>

I wouldn't count on any browser retaining any existing query string in the action URL.

As the specifications (RFC1866, page 46; HTML 4.x section 17.13.3) state:

If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type. Maybe one could percent-encode the action-URL to embed the question mark and the parameters, and then cross one's fingers to hope all browsers would leave that URL as it (and validate that the server understands it too). But I'd never rely on that.

By the way: it's not different for non-hidden form fields. For POST the action URL could hold a query string though.

Bhupinder kumar
  • 760
  • 5
  • 19
0
  • Assigning a value to req.params via an html form is not inherently feasible. Forms assign values easily to req.query instead.

  • Definitions for req,params and req.query described in Express 4.x API Reference:

req.params
This property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /user/:name, then the “name” property is available as req.params.name. This object defaults to {}.

req.query
This property is an object containing a property for each query string parameter in the route. If there is no query string, it is the empty object, {}.

  • Arjan's answer in this question cites hidden type inputs which could be helpful in certain use cases.
Community
  • 1
  • 1
stefaleon
  • 81
  • 1
  • 10