问题
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