如果我不想在帖子请求中对值进行硬编码怎么办?

时间:2019-04-17 07:07:44

标签: python django

目前,我只是在发出发布请求时将值硬编码为有效负载和标头,并且工作正常。但是现在,我不想将这些值硬编码到有效负载中并发出请求。.它应该在动态发出请求时接受这些值。

有人知道吗?

@csrf_exempt
@api_view(['POST'])
def addtocart(request):
    payload = dict(customer_key='c_5c88d447dde56', hub_id=4, product_id='pr_572359462392e', quantity=1)
    headers = {"content-type":"application/json", "token": "tk_ju9fdm8e", "source":"android"}
    response = requests.post('http://api/cart/update/v2', data=json.dumps(payload), headers=headers)
    return HttpResponse(response)

1 个答案:

答案 0 :(得分:-1)

@csrf_exempt
@api_view(['POST'])
def addtocart(request):
    # Query dictionary
    payload = request.data # <QueryDict: {'customer_key': [''], ...}>
    headers = {"content-type":"application/json", "token": "tk_ju9fdm8e", "source":"android"}
    response = requests.post('http://api/cart/update/v2', data=json.dumps(payload), headers=headers)
    return HttpResponse(response)