Send POST form with python

Asked

Viewed 3,482 times

1

I am developing an API in my work, and I need to develop something in python2 that sends a form to the server through the POST, I saw some questions here on the forum and I could not find anything that worked ... I need the community to help me!

Well, I’m using this code to understand the logic and pass it on to the senior project programmer:

import json
import urllib2

data = {
    'id' : 1, 'number': 556291406183, 'message' : 'Hello'
}

req = urllib2.Request('http://lsweb.net.br/gabriel.php')
req.add_header('Content-Type', 'application/json')

response = urllib2.urlopen(req, json.dumps(data))

But I’m not getting results with this code, so it doesn’t send anything to the server. The intention was to send to the server page in POST and POST to read in PHP. HELP ME

  • Buddy, this is Sopt please translate your question! I see that you already have a while you are registered in the community, so I recommend you to do the Tour.

1 answer

0


#Instale a biblioteca requests pip3 install requests urllib3 --upgrade

import requests
payload = {'username': 'meuusername123', 'password': 'senha1234'} #1

resposta = requests.post("http://httpbin.org/formulario-post", data=payload) #2

# 1 After detecting the HTML code fields that need to be filled in, let’s say it was a login form with user and password, group the data to use them in the POST request

#2 The post method of the requests module is used to send the form data. Where you should know through a basic code analysis which url to send the POST method.

A real example with the website Pastebin.com (Sending login data)

URL where the form is contained: https://pastebin.com/login Form Tag Values Contained:

Campo de usuário: user_name
Campo de senha: user_password
URL para envio do formulário: /login.php # onde completaremos do o domínio pastebin.com

Would that way:

import requests
payload = {'user_name': 'meulogin', 'user_password': 'minhasenha123'}
resposta = requests.post("https://pastebin.com/login.php", data=payload)
#print(resposta.text)

Raw HTML code

<form class="login_form" id="myform" method="post" action="/login.php">
...
</form>


<input type="text" name="user_name" maxlength="20" size="20" value="" 
...
</input>

<input type="password" name="user_password" size="20" value="" placeholder="Your 
...
</input>

Browser other questions tagged

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