Python interface test-send a GET request using the Requests module

Source: Internet
Author: User

This article mainly records the implementation code that uses Python's requests module to send a GET request.

To send a GET request to the server:
No parameters: R = Requests.get (URL)
With params: R = Requests.get (url,params=params)
With params and headers: R = Requests.get (url,params=params,headers=headers)

The code is as follows:
#Coding=utf-8ImportUnitTestImportRequestsclassgettest (unittest. TestCase):defsetUp (self): host='https://httpbin.org/'Endpoint='Get'Self.url="'. Join ([Host, endpoint])deftest1 (self): U" "get no parameter test" "R1= Requests.get (Self.url)#send a request to the serverCode = R1.status_code#Status CodeSelf.assertequal (200, code)Print(R1.text)#Unicode Type text    deftest2 (self): U" "get band parameter test" "params= {'show_env':'1'} R2= Requests.get (self.url,params=params) self.assertequal (200, R2.status_code)deftest3 (self): U" "get with parameters, with headers test" "params= {'show_env':'8'} headers= {'Connection':'keep-alive','accept-encoding':'gzip, deflate',                   'Accept':'*/*','user-agent':'python-requests/2.18.3'} R= Requests.get (Self.url, params=params,headers=headers) R3=R.json ()Print(R3) Connect= R3.get ('Headers'). Get ('Connection') self.assertequal ('Close', connect)#断言Check the connection value in the header    defTearDown (self):Passif __name__=="__main__": Unittest.main ()

Python interface test-send a GET request using the Requests module

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.