简体   繁体   中英

Testing a django post view with curl

I have the following POST method in django view.

def create_rule(request, rule_name, threshold, alert_value):
rule_name = request.GET.get('rule_name')
threshold = request.GET.get('threshold')
alert_value = request.GET.get('alert_value')
if request.method == 'POST' and rule_name is not None:
    user = get_current_user(request)
    rule = models.Rule(name=rule_name, user=user, threshold=threshold, 
                       alert_value=alert_value, is_internal=False)
    rule.save()

Now I am trying a curl from command line as follows:

curl --data "rule_name=TOTAL_REQUESTS&threshold=2&alert_value=2" http://localhost:8000/rules/create/

It throws an error telling.

Exception Type: TypeError at /rules/create/ Exception Value: create_rule() takes exactly 4 arguments (1 given)

I am passing the arguments from curl . Why is that the post is not receiving those.

You are getting the parameters from the request, but you have also defined them as method arguments, you should remove them from the signature:

def create_rule(request):
    rule_name = request.GET.get('rule_name')
    threshold = request.GET.get('threshold')
    alert_value = request.GET.get('alert_value')
    if request.method == 'POST' and rule_name is not None:
        user = get_current_user(request)
        rule = models.Rule(name=rule_name, user=user, threshold=threshold, 
                       alert_value=alert_value, is_internal=False)
        rule.save()

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