0
Pass INPUT value to URL API
I have an account on an external IP system that has an API that goes through the URL.
For example, if I want to authorize an IP, just type in the browser...
https://MEUSITE.com/proxy/dashboard/api/ips/add/usuarioAPI/SenhaAPI/123.123.123.123
...that it will add the Ip 123... in the database. Because, the user and password are already in the URL.
So I need to create a form like the one below to inform this IP and that when sending it run the URL.
Below is the form:
<form method="get" action="https://MEUSITE.com/proxy/dashboard/api/ips/add/usuarioAPI/SenhaAPI/{IP}">
<p>Adicionar IP: <input type="text" name="IPadicionar"></p>
<p><input type="submit" value="Enviar"></p>
</form>
I tried the default way, which was to put the variable at the end of the url - /?Ipadicionar - but it didn’t work. Because, the url has to have only the IP at the end and it can’t have ?
or =
.
So my question is how do I, when clicking submit, pass only the IP typed in the input to the end of the URL?
Thank you so much for returning. It adds the IP perfectly however returns the error ( error in request:failed to fetch )
– Anderson Girardi
If the API URL is on your domain, make sure your host is configured to allow JS requests and if the protocol in the URL (http / https) is the same on the JS page and the API URL, also check the request response code, you can use the Inspector /
DevTools
(F12 key or control shift + i) from the browser, in the Network tab to check. If the API is not on your domain check whether JS requests from other domains are allowed (Cross Origin Resource Sharing
), if it is not necessary to create an own PHP that requests with CURL for example to be called by your JS.– Zimaldo Junior
As an option to JS Fetch, you can also use Xmlhttprequest: Exmplo https://www.w3schools.com/js/js_ajax_http.asp Details: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
– Zimaldo Junior
These two options you have passed, ( Zimaldo Junior ) however because it is <script> if the user sees the source code of the page, he will be able to view the user and password of the api that is passed in the URL. would have some alternative to hide?
– Anderson Girardi
To hide this information you can create a PHP script to make the main request via CURL, and in JS instead of calling the API endpoint directly, make a request to your PHP and send the IP that will be added to the permission list. Here’s how to use CURL in PHP: https://imasters.com.br/back-end/curl-com-php-para-apis-restfull
– Zimaldo Junior