How to Set a Header Field on Post a Form

How to set a Header field on POST a form?

It cannot be done - AFAIK.

However you may use for example jquery (although you can do it with plain javascript) to serialize the form and send (using AJAX) while adding your custom header.

Look at the jquery serialize which changes an HTML FORM into form-url-encoded values ready for POST.

UPDATE

My suggestion is to include either

  • a hidden form element
  • a query string parameter

How to submit a HTML form with header

I want to submit a form including a header during posting the request , is that possible ?

No.

You can only add custom HTTP headers to a request if you are using XMLHttpRequest or fetch to make the request (so you can't if you are using a form submission to make the request).

You could use the submit event to intercept the form submission and make the request with XMLHttpRequest or fetch instead … but then you'd need to handle the response with JS too.

Add custom header on non-ajax post

Yes, the sentence implies that the POST operation is invoked by a custom JavaScript handler in order to inject the AUTH header. I've corrected the OWASP description to reflect this oversight.

How to set custom header on form-data item?

I found the solution myself, and its very simple.
Its posible to set custom headers in the options object header property

router.get('/getdata', async (req, res, next) => {
var form = new FormData();
var encodedImage2 = fs.readFileSync('./public/image2.png');
var options = {
header: {
'X-Custom-Header': 123
}
};
form.append('image2.png', encodedImage2, options);

res.set('Content-Type', 'multipart/form-data; boundary=' + form.getBoundary());
form.pipe(res);
});


Related Topics



Leave a reply



Submit