Answer the question
In order to leave comments, you need to log in
How to make a correct simple GET \ POST request in JS with the transfer of a header or other data?
Making a GET\POST request from python to FLASK
r = requests.post('http://127.0.0.1:5001/requestajaxtest', data = {'key':'value'})
print(r.url)
127.0.0.1 - - [17/May/2021 23:29:08] "POST /requestajaxtest HTTP/1.1" 200 -
POST: POST
ImmutableMultiDict([])
<bound method TypeConversionDict.get of ImmutableMultiDict([('key', 'value')])>
async function alerttest1() {
let response = fetch('http://127.0.0.1:5001/requestajaxtest', {
headers: {
Authentication: 'secret'
}
});
};
async function alerttest2() {
let user = {
name: 'John',
surname: 'Smith'
};
let response = await fetch('http://127.0.0.1:5001/requestajaxtest', {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(user)
});
let result = await response.json();
alert(result.message);
};
POST: GET
ImmutableMultiDict([])
<bound method TypeConversionDict.get of ImmutableMultiDict([])>
Answer the question
In order to leave comments, you need to log in
upd: If you send JSON - you need to receive them a little differently, request.form
they will not be in them, it does not accept JSON data. In the case of your queries via python, you were passing in a normal application/x-www-form-urlencoded
, for which the c variant request.form
is suitable. When you send requests through a JS script, you send them already in a form application/json
that can be received request.json
throughrequest.headers.get('Authentication')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question