Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function/method help
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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) ??
Reply
#4
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  i want to use type= as a function/method keyword argument Skaperen 9 1,838 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Building a method name in a function ffgth 9 3,190 Oct-19-2020, 01:21 PM
Last Post: buran
  Accessing method as function object ClassicalSoul 2 2,008 Feb-14-2020, 09:31 PM
Last Post: wavic
  function vs method prateekshaw 2 2,184 Nov-14-2019, 07:00 PM
Last Post: DeaD_EyE
  I'm trying to figure out whether this is a method or function call 357mag 2 2,420 Jul-04-2019, 01:43 AM
Last Post: ichabod801
  function method problems drchar 2 2,897 Dec-11-2018, 02:38 PM
Last Post: buran
  Understanding Method/Function robdhunter 2 2,635 Mar-10-2018, 11:57 PM
Last Post: robdhunter
  How to pass value from method to function iFunKtion 11 8,784 May-23-2017, 05:06 PM
Last Post: iFunKtion
  init vs_init_ while defining method/function? hsunteik 1 3,641 Dec-24-2016, 08:27 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020