Trying to recreate a Python API call request in JavaScript using Fetch with FormData

隐身守侯 提交于 2021-02-11 05:29:53

问题


So I am trying to recreate an API request in JavaScript as it is currently done in Python.

Here is the part of the Python program which matters:

        self.data = {'id': username,
                     'pass': password}

        self.session_id = self.post(base_url + loginURLtemplate,
                                    data=self.data).headers

So essentially the POST request returns everything I need. It returns a sessionid as well as a connection value of 'Keep-Alive'. This is the same behaviour as in Postman, or the browser.

Now when it comes to the JavaScript program I have wrote:

        var formData = new FormData()
        formData.append('id', username)
        formData.append('pass', password)
        var url = base_url

        fetch(url, {
            method: 'POST',
            body: formData,
            credentials: 'include',
            keepalive: true

        })
            .then(r => r)
            .then(data => {
                console.log(data.headers) 
            })

It returns a similar reply as the Python request but this time it omits the sessionid as well as the connection value is set to 'close' rather than 'Keep-Alive'

I am wanting to know if this is due to an error in something I have written OR if this is a drawback in using the Fetch Web API in JavaScript. It seems to successfully hit the server but receives a reply which does not act like a browser sent the request (like the Python request does)

来源:https://stackoverflow.com/questions/60804880/trying-to-recreate-a-python-api-call-request-in-javascript-using-fetch-with-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!