0

I want to make an HTTP POST request from an HTML anchor tag that has the URL in its href attribute.
How can I make it use POST instead of GET?

<a href='http:/test_url:5002/api/GetFile'></a>

I also want to set parameters in the body of the request. How can I do that?
I want to find a way to include them in the HTML tag itself, not in a JavaScript file.

tom
  • 21,844
  • 6
  • 43
  • 36
  • 1
    Does this answer your question? [Make a link use POST instead of GET](https://stackoverflow.com/questions/3915917/make-a-link-use-post-instead-of-get) – SMAKSS May 29 '20 at 13:00

4 Answers4

0

Links are designed to GET a URL. You cannot make a POST request directly with them.

Use a form with a submit button instead.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Thats what forms are for. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form Just create a form with a submit button. You can also specify if you want to open the requested URL in a new tab with the target attribute.

Timon
  • 359
  • 3
  • 10
0

This is a way you could do it

 <form  method="POST" action="/api/PostFile">

<!-- with input in between -->


<!-- and a submit button--> 

</form>
iSpark
  • 65
  • 8
0

there's only one way for you to POST data with a link... you would need to wrap the code in a <form> and use that link to submit the form, for example:

<form id="frm" ation="http:/test_url:5002/api/GetFile">
  <input type="hidden" name="param1" value="value_for_param1" />
  ...
  <a href="#" onclick="document.querySelector('#frm').submit()"></a>
  ...
</form>

any input can be used to append parameters to the <form>, as an example, the hidden is used

now, remember that you shouldn't use http:/test_url:5002 just minimize to /api/getfile or you would most likely get caught into CORS issues

balexandre
  • 73,608
  • 45
  • 233
  • 342