Dict with repeated Python attributes

Asked

Viewed 35 times

0

Good afternoon! I’m putting together a formdata for a post,

    formdata = {
    'data': '',
    'controle': 'ADMIN',
    'g-recaptcha-response': recaptcha_response
    }
    for numero in nDams:
        formdata['nu_dam[]'] = numero

i need the attribute nu_dam[], to be repeated as many times as necessary, logically as above, in the result there is only 1 num_dam[], always the last of the list. Have some way for the end of the formdata to be +/- like this

        formdata = {
        'data': '',
        'controle': 'ADMIN',
        'g-recaptcha-response': recaptcha_response,
        'nu_dam[]' : '123456',
        'nu_dam[]' : '123457',
        'nu_dam[]' : '123458',
        'nu_dam[]' : '123459'
    }

Thanks for your attention!

  • use a list as value

1 answer

1


Probably this:

formdata = {
    'data': '',
    'controle': 'ADMIN',
    'g-recaptcha-response': 'recaptcha_response',
    'nu_dam' : [
        '123456',
        '123457',
        '123458',
        '123459'
    ]
}

But it depends on the HTTP lib you are using, at the bottom this is an array name="nu_dam[]" for payload or querystring if coming from an HTML, ie is not that you will work like this, this is only in HTML.

To add use append:

formdata = {
   'data': '',
   'controle': 'ADMIN',
   'g-recaptcha-response': 'asdasd',
   'nu_dam': []
}


for numero in nDams:
    formdata['nu_dam'].append(numero)
  • opa alterei formdata['nu_dam[]'].append(numero) and it worked! thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.