简体   繁体   中英

How do I make a POST request from within a django function to rest api?

I have xyz.py file. In this file I have a function

@api_view(['POST']) 
def abc(request):

which can be accessible at a url, say /algorithms/abc .

I have another function

def pkr():

I want to make POST request from this function to abc(request) . I tried requests.request('POST', "/algorithms/abc", data=data_input) but the request.data at abc(request) is received as QueryDict (& not dict) & looses some texts that existed in data_input inside pkr() function.

Just to clarify what @Daniel_Roseman and @Hugo_Luis_Villalobos_Canto is saying in comment.

We can abstract (refactor out) the logic to process the data in some separate function and then re-use the same function at both places by passing data to it.

Something like this :

def process_data(data):
    # process your data here
    print(data)


@api_view(['POST']) 
def abc(request):
    data = request.POST
    process_data(data)


def pkr():
    process_data(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM