Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
InterfaceAutoTest
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
TestAuto
InterfaceAutoTest
Commits
f8fdf32c
Commit
f8fdf32c
authored
May 09, 2021
by
jiaqiying
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
request_main 兼容不同的headers content_type和mimitype特殊字段
parent
7220dd74
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
12 deletions
+32
-12
common/tools.py
+32
-12
No files found.
common/tools.py
View file @
f8fdf32c
...
...
@@ -9,23 +9,43 @@ from config import *
def
request_main
(
url
,
headers
,
method
,
data
):
"""封装requests的通用请求方法"""
res
=
None
def
request_by_method
(
method
,
headers
):
inner_res
=
None
try
:
header_content_type
=
headers
[
"Content-Type"
]
except
KeyError
:
header_content_type
=
headers
[
"mimeType"
]
try
:
if
method
.
upper
()
==
"GET"
:
inner_res
=
requests
.
get
(
url
=
url
,
headers
=
headers
,
params
=
data
)
elif
method
.
upper
()
==
"POST"
:
if
header_content_type
==
"application/json"
:
inner_res
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
elif
header_content_type
in
[
"application/x-www-form-urlencoded"
]:
inner_res
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
data
)
return
inner_res
except
Exception
as
e
:
logging
.
log
(
str
(
e
))
raise
Exception
if
headers
==
None
or
headers
==
{}
or
headers
==
""
:
# 如果传的headers为空,使用各自产品的通用headers
headers
=
get_headers
()
header_content_type
=
headers
[
"Content-Type"
]
try
:
if
method
.
upper
()
==
"GET"
:
res
=
requests
.
get
(
url
=
url
,
headers
=
headers
,
params
=
data
)
elif
method
.
upper
()
==
"POST"
:
if
header_content_type
in
[
"application/x-www-form-urlencoded"
]:
res
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
data
)
elif
header_content_type
==
"application/json"
:
res
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
except
Exception
as
e
:
res
=
request_by_method
(
method
,
headers
)
except
requests
.
exceptions
.
ConnectionError
as
e
:
logging
.
log
(
str
(
e
))
raise
Exception
except
requests
.
exceptions
.
RequestException
as
e
:
logging
.
log
(
str
(
e
))
if
"mimeType"
in
headers
.
keys
():
headers
[
'mimeType'
]
=
"application/x-www-form-urlencoded"
else
:
headers
[
'Content-Type'
]
=
"application/x-www-form-urlencoded"
try
:
res
=
request_by_method
(
method
,
headers
)
except
requests
.
exceptions
.
RequestException
as
e
:
logging
.
log
(
str
(
e
))
if
res
!=
None
:
return
res
.
json
()
return
res
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment