Allows you to create a custom HTTP request with any method supported by HTTP.
URL- 請求地址
Method – 請求方法 POST or GET.
EncType – 編碼型別,指定Content-Type,如"text/html","application/json"等,會重寫 web_add_[auto_]header中定義的Content-Type。
RecContentType – 響應頭編碼型別(Content–Type) e.g., text/html, application/x–javascript.
Body – 請求體,不同的應用中,請求體分別通過Body、BodyBinary或者BodyUnicode引數來傳遞
Resource – 指示URL是否屬於資源。1 是;0 不是。設定了這個引數後,RecContentType引數被忽略。
"Resource=1":意味著當前操作與所在指令碼的成功與否關係不大。在下載資源時如果發生錯誤,是當作警告而不是錯誤來處理的;URL是否被下載受“Run-Time Setting—Browser Emulation--Download non-HTML resources” 這個選項的影響。此操作的響應資訊是不做為HTML來解析的。
"Resource=0" :表明此URL是重要的,不受傳送請求(RTS)的影響,在需要時也會解析它。
Mode – 錄製級別: HTML or HTTP.
UserAgent – 使用者代理,它是一個HTTP頭的名字,用來標識應用程式,通常是瀏覽器,它呈現的是使用者和伺服器的互動。
簡單示例:
Action()
{
//GET 請求
web_custom_request("get_login",
"URL=http://10.1.102.75:8000/login?user=Milton&pwd=Loveyp",
"Method=GET",
"Resource=0",
"Mode=HTML",
"RecContentType=application/json",
LAST ); //POST 請求提交form資料
web_custom_request("post_form_login",
"URL=http://10.1.102.75:8000/login",
"Method=POST",
"Resource=0",
"Mode=HTML",
"Body=user=Milton&pwd=Loveyp",
LAST );
//POST 請求提交json資料
web_custom_request("post_json_login",
"URL=http://10.1.102.75:8000/json_login",
"Method=POST",
"Resource=0",
"Mode=HTML",
"EncType=application/json",
"Body={\"user\":\"Milton\",\"pwd\":\"Loveyp\"}",
LAST ); return 0;
}
執行後,通過View-》Test Results檢查請求結果
為了測試方便,這裡附上服務端介面程式碼,如下:
@csrf_exempt
def login(request):
if request.method == "POST":
user = request.POST.get("user")
pwd = request.POST.get("pwd")
else:
user = request.GET.get("user")
pwd = request.GET.get("pwd")
if user == "Milton" and pwd == "Loveyp":
msg = {
"code": 1000,
"msg": "login success! Welcome~~",
}
else:
msg = {
"code": -1,
"msg": "username or password error,please try again!",
}
response = JsonResponse(msg)
return response @csrf_exempt
def json_login(request):
user=""
pwd=""
if request.method == "POST":
print request.body
recive=json.loads(request.body)
print recive
print type(recive)
user=recive.get("user")
pwd=recive.get("pwd") if user == "Milton" and pwd == "Loveyp":
msg = {
"code": 1000,
"msg": "login success! Welcome~~",
}
else:
msg = {
"code": -1,
"msg": "username or password error,please try again!",
}
response = JsonResponse(msg)
return response

***微信掃一掃,關注“python測試開發圈”,瞭解更多測試教程!***