Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a request with Django view using Python-Requests

I'm trying to create a simple microservice structure on my Django projecjt: so when a certain Django view is called, this view will send a JSON post request to a Flask microservice containing some user's data; the Flask microservice should receive the request, take that user's data and send back to Django some additional data using Requests again, so that my Django view can receive that data and perform some operations, such as showing it to the user.

Right now i'm just sending some dummy data, to test if this whole system ( Django > request to Flask > Flask > Request to Django) works, but i'm having some problems.

To debug my code, i'm trying to just print the received data. Here is my view:

def myView(request):

    mydict = {}

    # The request is sent to my external Python script..
    req = requests.post('http://127.0.0.1:5000/', json={"one": 1}) # Some dummy data

    # .. Once the external script sends back a request with data, this should handle it
    if request.method == 'POST':

        # The data is inside this variable
        data = request.POST

        for key in data.items():
            if float(key[1]) > 0:
                mydict.update({key[0]: key[1]})

        print(mydict) #FIRST PRINT STATEMENT

    print(mydict) #SECOND PRINT STATEMENT
    response = HttpResponse(get_token(request))
    return JsonResponse(mydict) #RETURNS "{}"

And here is how my Flask app sends data (once it receives the POST request from the Django view) using the Python-Requests library:

@app.route("/", methods=["GET","POST"])
def test():
    # After the request from the VIEW is received, a request containing some random json data
    # is sent to Django
    url = 'http://127.0.0.1:8000/myView/'
    client = requests.session()

    # We need to get the CSRF token, in order for the request to be taken by Django
    csrftoken = requests.get(url).cookies['csrftoken']
    data = {"two": 2}

    header = {'X-CSRFToken': csrftoken}
    cookies = {'csrftoken': csrftoken}
    resp = requests.post(url, data=data, headers=header, cookies=cookies)
    # Let's seend the request to Django

    return f"Test!"

Here is what's wrong with my code:

METHOD:  POST
{'two': 2}
[10/Jan/2020 10:41:37] "POST /myView/ HTTP/1.1" 200 320
{}
[10/Jan/2020 10:41:37] "GET /myView/ HTTP/1.1" 200 2

Here is what's wrong: Why does the first print statement return the correct data and why doesn't the second print? And why does return JsonResponse return an empty dictionary?

I tried to add print('METHOD: ', request.method) in the first lines of the view, here is what happened:

METHOD:  GET
METHOD:  GET
[10/Jan/2020 10:46:22] "GET /myView/ HTTP/1.1" 200 2
METHOD:  POST
[10/Jan/2020 10:46:26] "POST /myView/ HTTP/1.1" 200 320
[10/Jan/2020 10:46:26] "GET /myView/ HTTP/1.1" 200 2
like image 830
Jack022 Avatar asked Oct 15 '22 07:10

Jack022


1 Answers

You're flask view can be simplified to just return the required data

@app.route("/", methods=["GET","POST"])
def test():
    return {"two": 2}

Then you can use the data in the Django view after you have made the request to flask

response = requests.post('http://127.0.0.1:5000/', json={"one": 1})
print(response.json()) # This should contain the returned data
like image 74
Iain Shelvington Avatar answered Oct 19 '22 00:10

Iain Shelvington