Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to transmit parameters according to parameter location, data type and different ways when sending Post request by Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to transmit parameters according to the location of parameters, data types, and different ways to send Post requests by Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. verify the SSL certificate

The request from https has a high level of security compared to http and requires verification of the SSL certificate.

The picture is from the Internet.

Https's request, when you open fiddler, running the code at the same time will report an error, indicating that there is no such certificate. You can ignore this certificate and add verify=False. But there is still a warning, you can write:

Import urllib3 urllib3.disable_warnings ()

To ignore the warning.

Reliable link: https://blog.csdn.net/qq_35304570/article/details/79826990

II. Post request

1. Assertion

Assertions are commonly referred to as checkpoints.

The difference between an assertion and an if: an assertion throws an exception when it is not equal. No exception is thrown when it is equal. If statement is printed out for you to take a look at.

Import requests import urllib3 urllib3.disable_warnings () url=' http://japi.juhe.cn/qqevaluate/qq' b = {"key": "1cb0de9717e554cef5f3539285b99726", "qq": "169535"} r=requests.post (url,data=b) print (r.text) # after conversion into a dictionary Then take the value reason=r.json () ["reason"] print (reason) # actual result exp= "success" # expected result # assertion # assert reason==exp if reason==exp: print ("test passed") else: print ("test failed")

The if statement can also throw an exception, such as raise NameError.

Import requests import urllib3 urllib3.disable_warnings () url=' http://japi.juhe.cn/qqevaluate/qq' b = {"key": "1cb0de9717e554cef5f3539285b9972", "qq": "169535"} r=requests.post (url,data=b) print (r.text) # after conversion into a dictionary Then take the value reason=r.json () ["reason"] print (reason) # actual result exp= "success" # expected result # assertion # assert reason==exp if reason==exp: print ("test passed") else: raise NameError

Exceptions can be thrown if you want to, or not if you don't want to.

two。 Post request with parameters in body

The parameters in body are in the format of key-value pairs.

The picture is from the Internet.

3. Post requests whose parameters are not placed in body

The parameters of the post request can also be placed in params:

This is not put casually. If the parameters are not specified in the API document, you can put it in params or body.

The picture is from the Internet.

4. Which parameters are put in Query String and which parameters are put in body?

Grab the packet, if you see the parameters of the interface in url? After the number, use params to pass it, and if it is under the empty line in the header, use data to pass it.

5. If there are two places at the same time, then you can send two at the same time, one by one.

The picture comes from the Internet, and the text on the picture is edited by the editor.

Import requests import urllib3 urllib3.disable_warnings () url=' http://japi.juhe.cn/qqevaluate/qq' p = {"key": "1cb0de9717e554cef5f3539285b99726", "qq": "169535"} body= {"xx": "xxx"} # Content-Type:application/x-www-form-urlencoded r=requests.post (url,params=p,data=body) print (r.text) # after conversion into a dictionary Then take the value reason=r.json () ["reason"] print (reason) # actual result exp= "success" # expected result # assertion # assert reason==exp if reason==exp: print ("test passed") else: print ("test failed")

Summary: some post request parameters are in url, some in body, and some in url and body.

When to send a get request and when to send a post request is determined by the development documentation.

Third, if the format of Content-Type is different, the parameters will be passed in different ways.

1. When writing a post request, you should pay attention to which data type it is.

There are four common ones, not just four.

1. The first is application/json: {"key1": "value1", "keyt2": "value2"}

Json=

two。 The second is application/x-www-form-urlencoded:name1= value1&name2=value2.

Data=

3. The third is multipart/form-data: this one is in form format (file upload, file=, picture upload, etc.)

Data=

4.Content-Type:octets/stream (file download)

Data=

5.text/xml

Data=

two。 Pass parameters in different ways

The picture is from the Internet.

Get requests are also in this format, mainly because post requests need to be distinguished.

Post request: if it is a parameter in the format of application/x-www-form-urlencoded, use data=,. If it is a parameter in the format of application/json, use json=.

Highlight: only Content-Type is the parameter of application/json format, then use json=,. All other format parameters use data=. The parameters above url still use params=.

Post request in 3.json format

The parameter type of the request body is declared under the header.

Send a request with fiddler:

The code can be written as follows:

The first parameter is url. This position cannot be changed at will. The position of the following parameter can be changed at will, as long as the parameter name is specified.

Import requests url= "http://www.example.com/" h = {" Content-Type ":" application/json "} body= {" key ":" 1cb0de9717e554cef5f3539285b99726 "," qq ":" 169535 "} # Content-Type:application/json json= r=requests.post (url,headers=h,json=body) print (r.text)

When using fiddler: ctrl+x can empty the package caught by fiddler. Pause click Capturing.

This is the end of the content of "how to send Post requests by Python to pass parameters, data types and different ways according to the location of parameters". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report