Python POST请求迭代

时间:2017-11-25 11:23:33

标签: python post request python-requests faker

我尝试使用以下代码在循环中发出POST请求

description = fake.catch_phrase()
group_id = ''
invite_only = 1 if fake.boolean(chance_of_getting_true=50) == True else 0
is_public = 1 if fake.boolean(chance_of_getting_true=50) == True else 0
title = fake.company()

payload = {description,
       group_id,
       invite_only,
       is_public,
       title}
response = requests.post(createGroup, data=(payload), headers=headers)

我明白了 服务器上的SequelizeValidationError: notNull Violation: v_title cannot be null 但是,如果我尝试像这样发送相同的有效载荷

payload = {'description': 'abc',
          'group_id': '1',
          'invite_only': '1',
          'is_public': '1',
          'title': 'someTitle'}

完美无缺。

因此,问题是我可以在邮件调用中发送随机生成的数据吗? 如果可以,那怎么可能呢?

1 个答案:

答案 0 :(得分:0)

payload = {description, group_id, invite_only, is_public, title} 参数应该是字典,字符串或文件。但是,这是 set

payload = {'description': description,
      'group_id': group_id,
      'invite_only': invite_only,
      'is_public': is_public,
      'title': title}

所以通过这样的字典:

description = fake.catch_phrase()
group_id = ''
invite_only = int(fake.boolean(chance_of_getting_true=50))
is_public = int(fake.boolean(chance_of_getting_true=50))
title = fake.company()

并初始化这样的随机变量:

int()

(注意使用$(document).ready(function() { $('#example').DataTable(); } ); 将布尔值转换为1或0)