I am trying to learn how to take a body with data in the GET method, but for some reason nothing comes from me.

Here is the code:

from flask import Flask, request app = Flask(__name__) @app.route('/test') def get_test_data(): print('\nrequest:') print(request) print('request.data:') print(request.data) 

And here is the test code:

 import unittest from flask import url_for from proba import app class ProbaTest(unittest.TestCase): def setUp(self): self.app_ctx = app.app_context() self.app_ctx.push() self.client = app.test_client() def tearDown(self): self.app_ctx.pop() def test(self): self.client.get(url_for('get_test_data'), data={'username' : 'user1', 'password' : 'pass1'}) if __name__ == '__main__': unittest.main(verbosity=2) 

I receive:

 (env) d:\Projects\python-workspace\proba>python manage.py test test (proba_test.ProbaTest) ... request: <Request 'http://localhost/test' [GET]> request.data: b'' ok 

My main goal: To give out information on my resources only to those who are registered.

To do this, I write a decorator, which checks the username and password passed in the request. And this is a minimal example of repeating my situation.

Please tell me what am I doing wrong?

UPD : In the comments it was said that the body cannot be sent to GET. There is a discussion on this topic in the English version of stackoverflow: HTTP GET with request body

  • If you find a solution to a question, you can issue it as an answer. - Timofei Bondarev
  • @Timofey Bondarev: No, not found. - sys_dev

2 answers 2

A get request differs from a post request in that the get request has no body. therefore, you must either use either the post-request or transfer your data in get parameters, such as

 self.client.get('{0}?username=user1&password=pass1'.format(url_for('get_test_data'))) 
  • So how does HTTP Auth work? In the headlines? - sys_dev
  • @sys_dev Yes, the client sends the HTTP Authorization header with a (usually) base64-encoded password. (And authorization on websites, if anything, is usually done through cookies and sessions running on top of them, I’ve seen such functionality in Flask.) - andreymal
  • @andreymal: To understand your problem. First of all, I need to understand, and in the GET request to send the body, yes or no? Judging by the link that led after the UPD - you can. The question arises: why isn't it being sent from me? - sys_dev
  • @sys_dev because according to the standard it is impossible. And for that link about it is written. And maybe someone in the code cuts this body itself, trying to meet the standard. Probably, you can find some workarounds and this body to read, but still do not need to do so - the standard prohibits. - andreymal

The solution to my problem is to use the request.get_data () method instead of request.data.

There is a rather detailed explanation in the English-language version of Get raw POST body in Python Flask regardless of Content-Type header