0

I want to make a post API call from a anchor HTML tag and URL will be included in the href. How can i attach the body parameters.

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

And in this I want to send the body parameters in the call as well. I want to find a way to include this in the html tag itself, not in the javascript file.

Help will be appreciated.

  • 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
  • 857,932
  • 118
  • 1,152
  • 1,264
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
  • 197
  • 1
  • 8
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
  • 71,373
  • 44
  • 228
  • 330