python requests post application

Asked

Viewed 59 times

0

I am sending data via post to a web application made in Aravel with the CSRF active.

This is the form:

Note: I edited the _token.

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Upload</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="/posicoes">
    <input type="hidden" name="_token" value="abcdef">    <div>
    <span>MD5</span>
        <input type="text" id="md5" name="md5" maxlength="50" size="50">
    </div>
    <div>
        <input type="file" id="jsonFile" name="jsonFile">
    </div>
    <div>
        <button type="submit">Enviar</button>
    </div>
    <div>
    </div>
</form>
</body>
</html>

My shipping code is this:

def enviar(self, dados, path_post):
    md5 = dados['md5']
    path_arquivo = dados['arquivo']

    formulario = {}
    formulario['md5'] = md5

    url = 'http://' + self.host + path_post

    session = requests.session()
    form = session.get(url)
    csrf_token = re.findall(r'<input type="hidden" name="_token" value="(.*)"', form.text)[0]
    cookies = session.cookies
    
    formulario['_token'] = csrf_token
    
    files = {'jsonFile':('dados_gps.json', open(path_arquivo, 'rb'))}
    
    response = session.post(url, files=files, data=formulario, cookies=cookies)

    
    return response

but the answer is always the error 405.

  • Error 405 is method not allowed. already tried session.get?

  • Not because I need to send data to the server

  • post would be correct, but it depends on the server implementation. Again, 405 is not accepted method. That is, endpoint does not accept POST.

  • When I do manually or be paste the md5 into the field and select the file the post works correctly. Data is saved in the application normally. This is no proof that it is working properly?

  • The path the web application developer gave me was wrong. The post was in /positions and not in /positions/create. Thanks for the help.

  • I didn’t understand how manually it worked, but the path you passed was wrong... Still, I’m glad it worked.

Show 1 more comment
No answers

Browser other questions tagged

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