L
L
La1serg2019-07-27 14:11:03
JavaScript
La1serg, 2019-07-27 14:11:03

How to transfer cookies to another domain with a POST request?

You need to make a POST request to allpositions.ru/api and pass cookies to it. How to implement it in js?
Screenshot of the "documentation" of the service:
5d3c3122aaf0e280544976.png
My code:

receiveData() {
            document.cookie = `api_key=${token}`
            let data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>get_report</methodName><params><param><value><int>999999</int></value></param></params></methodCall>";

            fetch(
                'http://allpositions.ru/api/', {
                    method: 'POST',
                    mode: 'no-cors',
                    credentials: 'include',
                    headers: {
                        'Content-Type': 'text/xml',
                        'Access-Control-Allow-Origin': 'http://allpositions.ru',
                        'Access-Control-Allow-Credentials': true,
                        'Access-Control-Allow-Methods': 'GET, POST',
                        'Access-Control-Allow-Headers': 'Content-Type, *',
                        // 'Cookie': `api_key=${token}`
                    },
                    body: data,
                })
                .then(response => {
                    response.text().then(function (text) {
                        console.log(text);
                    });
                })
        }

Problems:
1. As a result, I get a warning in the console
Cross-Origin Read Blocking blocked cross-origin response

This was bypassed by launching the browser with the "--disable-web-security" parameter.
2. In response from allpositions.ru I receive information that the cookies are incorrect (in fact, they are correct, the service just does not receive them).
Actually the question is in the title.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2019-07-27
@ThunderCat

the browser sends cookies only to the domain from which the document is opened, for your case you need to use not a browser script, but for example cURL.

How to implement it in js?
no way, more precisely from the client side in the browser no way. You can use the node on the server, then it will be on js and what you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question