简体   繁体   English

如何使用 python 请求库发送此请求

[英]How can I send this request using python requests library

How to send the below request using python requests library?如何使用 python 请求库发送以下请求?

Request:要求:

要求

I have tried我努力了

with requests.Session() as session:
    // Some login action

    url = f'http://somewebsite.com/lib/ajax/service.php?key={key}&info=get_enrolled'
    json_data = {
        "index": 0,
        "methodname": "get_enrolled",
        // And so on, from Request Body
    }

    r = session.post(url, json=json_data)

But it doesn't give the output I want.但它没有给出我想要的 output。

1.Define a POST request method 1.定义一个POST请求方法

import urllib3
import urllib.parse

def request_with_url(url_str, parameters=None):
    """
    https://urllib3.readthedocs.io/en/latest/user-guide.html
    """
    http = urllib3.PoolManager()
    response = http.request("POST",
                            url_str, 
                            headers={ 
                                'Content-Type' : 'application/json' 
                            },
                            body=parameters)
    resp_data = str(response.data, encoding="utf-8")
    return resp_data

2.Call the function with your specific url and parameters 2.使用您的特定 url 和参数调用 function

json_data = {
        "index": 0,
        "methodname": "get_enrolled",
        // And so on, from Request Body
    }
key = "123456"
url = "http://somewebsite.com/lib/ajax/service.php?key={0}&info=get_enrolled".format(key)

request_with_url(url, json_data)

With no more info what you want and from what url it is hard to help but.没有更多信息您想要什么以及 url 是什么,很难帮助。 But try adding headers with the user-agent to the post.但是尝试将带有用户代理的标题添加到帖子中。 More headers may be needed, but User-Agent is a header that is often required.可能需要更多标头,但 User-Agent 是经常需要的 header。

with requests.Session() as session:
    // Some login action

    url = f'http://somewebsite.com/lib/ajax/service.php?key={key}&info=get_enrolled'
    json_data = {
        "index": 0,
        "methodname": "get_enrolled",
        // And so on, from Request Body
    }
    headers = {'User-Agent': 'Mozilla/5.0'}
    r = session.post(url, json=json_data, headers=headers)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能使用 Python 的 `requests` 库在 POST 请求中将 `None` 作为数据发送? - Why can't I send `None` as data in a POST request using Python's `requests` library? 如何在Python中使用“请求”库发送“ -X POST”请求? - How to send '-X POST' request using 'requests' library in Python? 如何使用 requests 库发送和接收请求? - How do I send and receive a request using the requests library? 如何使用请求库发送xml正文? - How can I send an xml body using requests library? 如何向我的 PC 上的 HTML 文件发送请求,就好像它是使用 Python/Requests 的真实服务器一样? - How can I send a request to an HTML file on my PC as if it were a real server using Python/Requests? 使用python请求库在HTTP请求中发送json - Using python requests library to send json in http request 如何使用Python请求库发送原始HTTP请求消息? - How to use Python requests library to send raw HTTP request message? 如何使用 Python 请求库在发布请求中发送 cookie? - How to send cookies in a post request with the Python Requests library? 如何使用 threadpoolexecutor 和请求在 python 中发送具有多个请求的有效负载? - how can i send payload with multiple requests in python using threadpoolexecutor and requests? 如何使用 Python 中的“请求”库发送“-X POST”? - How to send '-X POST' using 'requests' library in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM