Requests module Common functions

Source: Internet
Author: User

Can use Google Chrome postman to implement Interface simulation request


Default rules for function pass parameters:

Use*argsand the**kwPython is a customary way of writing, of course, you can use other parameter names, but it is best to use idioms.

1 GET Request

#通过api获取json返回值的, a general function

Def op_on_api (u, method= ' GET ',  **kw):    url =  u     r = requests.get (url, params=kw, timeout=10)     if  r.status_code != 200:        raise exception (' CORE API cannot process this URL: '  + url +  ' request!  return code: '  +                           str (R.status_code)  +                           ' \n\tmessage:{'  + r.content  +  '} '  +                           ' \n\tdata:==\n '  + str (args)  +   ' \n== ')   &nBsp; return r.json () 


GET request print r.url You can know the URL of the specific parameter of the request, which is HTTP just oh

U ' http://youapi?key2=value2&key1=value1 '

2 POST Request

import os,json, requests                                                                                                                                                    url =  "Http://youapi"                                                                                                                          payload={     "Key1": "Value1",      "Key2": "value2",}headers = {' Content-type ':  ' Application/json '}r =  Requests.post (Url, data=json.dumps (payload), headers=headers,timeout=10)


This article is from the "Tridewah operation and maintenance work Road" blog, please be sure to keep this source http://cuidehua.blog.51cto.com/5449828/1783991

Requests module Common functions

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.