0

For example username and password in

r = requests.post( loginUrl, data = {'username':'myuser', 'password':'mypw'} )

are from attribute name on the web site element. The information to post is given in parameter data with a dictionary.
It has a key username with a value myuser. The key username is taken from an element on a website with attribute name, means the key is taken from a key-value. By default requests select attribute name for the key.

To clarify this here is a html code snippet:

<form name="login" id="form1" method="post" action="http://mylogin.net">
  Username: <input name="username" id="input1" type="text" /><br/>
  Password: <input name="password" id="input2" type="password" /><br/>
  <input type="submit" value="OK" />
</form>

I prefer to use the id attribute as key because it is unique on the web page.

How can I select attribute id instead of default name to get the key for the post in requests, so I can use

r = requests.post( loginUrl, data = {'input1':'myuser', 'input2':'mypw'} ) ?
Hille
  • 1,763
  • 18
  • 29
Ingo
  • 473
  • 6
  • 19
  • 1
    all servers expect value from `name`, not from `id` - so you can't use `'input1':'myuser'` - you have to use `'username':'myuser'`. – furas Jan 02 '18 at 12:38
  • 1
    btw: use `DevTool` in Chrome/Firefox (tab `Network`) to see all requests send from browser to server and you will see that browser uses `'username'`, not `'input1'`. It doesn't send full `
    ` but only some values so you will not see `'input1'` in POST request.
    – furas Jan 02 '18 at 12:41
  • @furas Ah yes, I see. That give me the idea to google with the right keywords. Look at my answer. – Ingo Jan 02 '18 at 22:00

1 Answers1

0

As I learned from the discussion there is no solution. I found here and here that the name attribute is required for sending data to the server.

Use name attributes for form controls (such as <input> and <select>), as that's the identifier used in the POST or GET call that happens on form submission.

Ingo
  • 473
  • 6
  • 19