Python Interface Automation 2-send a POST request

Source: Internet
Author: User

Objective

Send POST Request Reference example is very simple, the actual situation is very complex, the first post request must be logged in, but login is the most difficult to handle. The login problem is solved, and it's easy to follow.

First, view official documents

1. Learn a new module, in fact, do not go to Baidu or anything, directly with the help function can see the relevant comments and case content.

>>import Requests

>>help (requests)

2. See the case of Python sending get and post requests

>>> Import Requests
>>> r = requests.get (' https://www.python.org ')
>>> R.status_code
200
>>> ' Python is a programming language ' in R.content
True

... or POST:

>>> payload = dict (key1= ' value1 ', key2= ' value2 ')
>>> r = requests.post (' http://httpbin.org/post ', data=payload)
>>> Print (R.text)
{
...
"Form": {
"Key2": "value2",
"Key1": "Value1"
},
...
}

Second, send the POST request

1. Use the above case, make a simple change, send a POST request

The 2.payload parameter is a dictionary type, which is passed into the form

Third, JSON

The body of 1.post is a JSON type and can also be passed in with JSON parameters.

2. First import the JSON module and use the Dumps method to convert to JSON format.

3. Return the result and upload it to data

Iv. headers

1. Take the Zen Road login as an example, simulate landing, here to add the request head headers, can be used fiddler grab bag

2. Write the request header in a dictionary format

headers = {"User-agent": "mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0 ",
"Accept": "Application/json, Text/javascript, */*; q=0.01 ",
"Accept-language": "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Accept-encoding": "gzip, deflate, BR",
"Content-type": "Application/json; Charset=utf-8 ",
"X-requested-with": "XMLHttpRequest",
"Cookie": "XXX ...", # here the cookie is omitted.
"Connection": "Keep-alive"
}

Five, Zen Road login Reference Code

1 #Coding:utf-82 #Coding:utf-83 ImportRequests4 #Zen Road host address5Host ="http://127.0.0.1"6 7 defLogin (S,USERNAME,PSW):8URL = host+"/zentao/user-login.html"9 Tenh = { One         "user-agent":"mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0", A         "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", -         "Accept-language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3", -         "accept-encoding":"gzip, deflate", the         "Referer": host+"/zentao/user-login.html", -         #"Cookie": # Do not use cookies until the head is logged in, as cookies are kept logged in -         "Connection":"keep-alive", -         "Content-type":"application/x-www-form-urlencoded", +         } -  +Body1 = {" Account": Username, A              "Password": PSW, at              "keeplogin[]":" on", -              "Referer": host+"/zentao/my/" -             } -  -     #s = requests.session () do not write dead session -  inR1 = s.post (URL, data=body1, headers=h) -     #return R1.content # Python2 return of this to     returnR1.content.decode ("Utf-8")#Python3 +  - defis_login_sucess (res): the         if "Login failed, please check that your user name or password is filled in correctly. " inchRes: *                 returnFalse $         elif "parent.location=" inchRes:Panax Notoginseng                 returnTrue -         Else: the                 returnFalse +  A if __name__=="__main__": thes =requests.session () +A = Login (s,"Admin","e10adc3949ba59abbe56e057f20f883e") -result =is_login_sucess (a) $     Print("test Result:%s"%result) $  -  

Python Interface Automation 2-send a POST request

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.