How to make a post request with the Python requests library?

前端 未结 4 1602
别跟我提以往
别跟我提以往 2020-12-29 12:12

I am using the following filters in Postman to make a POST request in a Web API but I am unable to make a simple POST request in Python with the requests library.

<

4条回答
  •  臣服心动
    2020-12-29 12:25

    I would recommend using the json attribute instead of data. It handles the dumping for you.

    import requests
    
    data = {'user_name':'user&001'}
    headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
    url = "http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets/"
    r = requests.post(url, headers=headers, json=data)
    

    Update, answer for question 3. Is there a reason you are using urllib? I’d use python requests as well for this request.

    import requests
    
    def get_json():
        r = requests.get("http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets”, params={"user_name": user_name.replace(" ", "&")})
    
        return r.json
    
    # not sure what you’re doing here, more context/code example would help
    def get_tickets_not_temp_degradation(start_date, end_date, complete_):
        return Counter([k['user_name'] for k in complete_data if start_date < dateutil.parser.parse(k.get('DateTime')) < end_date and k['T_subcategory'] != 'Temporary Degradation'])
    

    Also, is the username really supposed to be user+001 and not user&001 or user 001?

提交回复
热议问题