L
L
LS Timer2019-10-22 09:08:39
Java
LS Timer, 2019-10-22 09:08:39

POST request to a private API (has been blocked by CORS policy)??

Is it possible to connect to a closed API?
The resource accepts requests only from its own address, how can I get around this?
Change request headers?
Possible implementation in Java or kotlin

kotlin

fun test(): Any? {

        var link = "http://index.php"

        val url = URL(link)

        val con = url.openConnection() as HttpURLConnection
        con.requestMethod = "POST"
        con.setRequestProperty("Content-Type", "application/json")

        con.doOutput = true

        val out = DataOutputStream(con.outputStream)
        out.writeBytes("q=preschool")
        out.flush()
        out.close()

        BufferedReader(InputStreamReader(con.inputStream)).use {
            val response = StringBuffer()

            var inputLine = it.readLine()
            while (inputLine != null) {
                response.append(inputLine)
                inputLine = it.readLine()
            }
            it.close()
            println("Response : $response")
            return response
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2019-10-22
@light___soul

EMNIP, CORS only affects requests from browsers. The backend should work with the API without problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question