Python Forum

Full Version: function/method help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to python, trying to work on requests module

Need to use get and post method. But not sure what arguments are needed to be passed - Can you help me to find out.

python 3.5
import requests, inspect 
inspect.getfullargspec(requests.get) 
FullArgSpec(args=['url', 'params'], varargs=None, varkw='kwargs', defaults=(None,), kwonlyargs=[], kwonlydefaults=None, annotations={})
===But am looking for what params are allowed for each method
This url helps
https://www.w3schools.com/python/ref_requests_get.asp

The above URL shows me what am interested but trying to see a way to get those.

tried help/dir but no luck

Please help experts.
you can use help, but it will depend on the doc-string provided. Of course you can consult also the docs/tutorials provided by package author.
Inspecting just the signature will not always be helpful, because function may take arbitrary arguments, like in this case - *kwargs

so, back to help
help(requests.get)
Output:
Help on function get in module requests.api: get(url, params=None, **kwargs) Sends a GET request. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary, list of tuples or bytes to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. :return: :class:`Response <Response>` object :rtype: requests.Response
so, param *kwargs will hold the optional rguments that requets takes.
look at help(requests.request)

help(requests.request)
Output:
Help on function request in module requests.api: request(method, url, **kwargs) Constructs and sends a :class:`Request <Request>`. :param method: method for the new :class:`Request` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary, list of tuples or bytes to send in the body of the :class:`Request`. :param data: (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers to add for the file. :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth. :param timeout: (optional) How many seconds to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) <timeouts>` tuple. :type timeout: float or tuple :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``. :type allow_redirects: bool :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. :param verify: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to ``True``. :param stream: (optional) if ``False``, the response content will be immediately downloaded. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. :return: :class:`Response <Response>` object :rtype: requests.Response Usage:: >>> import requests >>> req = requests.request('GET', 'https://httpbin.org/get') <Response [200]>
Further you can inspect the source code and also the docs
Thanks for the reply

But I am interested in finding what are the args/kwargs GET method can take

But you said help(request.request)

How do I know it should be requests.requests

Assuming if I look for other module , will that be help(module.module) ??
(May-17-2020, 01:27 AM)myv5285 Wrote: [ -> ]But you said help(request.request)

How do I know it should be requests.requests

Because it's in the docstring for requests.get:
Quote: :param \*\*kwargs: Optional arguments that request takes.

Then, I looked at the source code, to confirm my understanding.


As I said - it's not always as simple as just using help() - it may be that function/method takes arbitarry arguments - e.g. *args, **kwargs, the docstring may be poorly written, etc. You may need to look at the source code or other resources like documentation (assuming it's not genereated from doc-strings), tutorials, etc.