Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'dict' object has no attribute 'encode'

I'm trying to post a request to the target website using multi part form data

m = MultipartEncoder(
    fields={"auth":{"id":str(random.randint(0, 999991)),"sign":randoms(32)},
    "data":{"action":"login","login":"embrella","password":"steffano321","stayLogged":"False"}
    })
s = requests.Session()
s.post('targetwebsite', data=m, headers=headers['Content-Type': m.content_type], timeout=5)

But I always end up with

line 25, in <module>
    "data":{"action":"login","login":"embrella","password":"steffano321","stayLogged":"False"}
AttributeError: 'dict' object has no attribute 'encode'

I don't know what's causing this, I've even tried converting to JSON first then posting

uuhh = {'auth':{"id":str(random.randint(0, 999991)),"sign":randoms(32)},
    "data":{"action":"login","login":"embrella","password":"steffano321","stayLogged":"False"}}
    fields = json.dumps(uuhh)
m = MultipartEncoder(fields=fields)

But it says ValueError: not enough values to unpack (expected 2, got 1) so I guess that is very wrong.

like image 723
Leo Avatar asked Oct 24 '25 18:10

Leo


1 Answers

It seems that you are passing auth and data as a dictionaries but instead they should be strings. Change

    m = MultipartEncoder(
    fields={"auth":{"id":str(random.randint(0, 999991)),"sign":randoms(32)},
    "data":{"action":"login","login":"embrella","password":"steffano321","stayLogged":"False"}
    })

to

    m = MultipartEncoder(
    fields={"auth":'{{"id":{},"sign":{}}}'.format(random.randint(0, 999991), randoms(32)),
    "data":'{{"action":"login","login":"embrella","password":"steffano321","stayLogged":"False"}}'
    })

to fix your issue.

MultipartEncoder tries to run .encode(...) method on the values of fields.
Since you're passing a dict, it fails to encode and the error

like image 169
Kasem Alsharaa Avatar answered Oct 26 '25 06:10

Kasem Alsharaa