egx
egx

Reputation: 399

Authentication Header for HTML Post form

Is it possible to parse/add an authentication on the submit from a standard HTML for? E.g. I'm using oAuth to authentication logged-in users, and have a usecase where I need to use a standard HTML form with the action and method attributes. But I can't seem to find a way to parse the JWT Token I'm using for authentication. Is this possible?

Upvotes: 2

Views: 3180

Answers (2)

Michal Trojanowski
Michal Trojanowski

Reputation: 12322

No, you can't do it with a regular HTML form. If you need to add an Authorization header to the request, the only thing you can do is to make a request from the Javascript (as is shown in Subhashis's answer). It doesn't have to be jQuery though, you can use plain JS and fetch or some libraries for making http calls (e.g. axios). Whichever you use, remember that it will be an AJAX call, so your JS will have to handle the response properly (the response will not be handled by the browser automatically).

Upvotes: 3

Subhashis Pandey
Subhashis Pandey

Reputation: 1538

You may use JQuery form submit to send authorization header

var form = $('#form-id').get(0); 
var formData = new FormData(form);

var xhr = new XMLHttpRequest();
xhr.open("POST", "url");
xhr.setRequestHeader("Authorization", "jwt token");
xhr.send(formData);

Upvotes: 1

Related Questions