Python requests post请求如何直接发送字符串参数

使用 requests.post 可以发送一个请求,参数是纯文本字符串。


requests.post 参数

def post(url, data=None, json=None, **kwargs):
    r"""Sends a POST request.

    :param url: URL for the new :class:`Request` object.
    :param data: (optional) Dictionary, list of tuples, bytes, or file-like
        object to send in the body of the :class:`Request`.
    :param json: (optional) json data to send in the body of the :class:`Request`.
    :param \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """

    return request('post', url, data=data, json=json, **kwargs)


从上面api可以看出,并没有直接传递字符串的方式。(期间用data、json、file-like各种试都不行)


但官方文档中有个说明:

There are many times that you want to send data that is not form-encoded. If you pass in a string instead of a dict, that data will be posted directly.


所以很简单,直接使用 data=str 就行。

res = requests.post(API_URL, data=url)


 

  

展开阅读全文