1. 程式人生 > >【Loadrunner】解決post json問題

【Loadrunner】解決post json問題



問題:傳送json的post請求時,雖然請求指令碼中有用到RecContentType=application/json;charset=UTF-8,但是回放時,日誌中提示: Error -26616: HTTP Status-Code=415 (Unsupported Media Type) for+請求地址,通過檢視請求的包頭和傳送值,以及抓包工具對比,發現RecContentType引數中定義了響應內容為json,但其實預設傳送的居然為:Content-Type: application/x-www-form-urlencoded,於是只能無奈的手工新增包頭引數,具體操作如下:
解決方法:
 
方法1:如果用web_custom_request傳送請求,自帶EncType引數,可以直接新增該引數值application/json;charset=UTF-8,即可解決此問題

web_custom_request("placeOrder",
  "URL=http://{IP}/appServer/interface/order/placeOrder",
  "Method=POST",
  "RecContentType=application/json;charset=UTF-8",
  "Snapshot=t1.inf",
  "Mode=HTTP",
  "EncType=application/json;charset=UTF-8",
  "Body={\"userId\":{userId},\"shopId\":{shopId},\"orderSceneType\":1,\"orderType\":1,\"goods\":[{\"shopId\":{shopId},\"goodsId\":{goodsId}}]}",
  LAST);

方法2:如果用提交表單(web_submit_data)的方式傳送請求,不自帶EncType引數,必須手動新增包頭,可解決此問題,但會保警告,大概意思是“新增包頭後,後續請求將用到”,此警告暫時未影響請求最終效果,可忽略。

 web_add_header("Content-Type","application/json; charset=utf-8");

web_submit_data("placeOrder",
        "Action=http://{IP}/appServer/interface/order/placeOrder",
        "Method=POST",
        "RecContentType=application/json;charset=UTF-8",
        "Snapshot=t1.inf",
        "Mode=HTML",
        ITEMDATA,
        "Name=data", "Value={userId}", ENDITEM,
        "Name=shopId", "Value={shopId}", ENDITEM,
        "Name=orderSceneType", "Value=1", ENDITEM,
        "Name=orderType", "Value=1", ENDITEM,
        "Name=goods", "Value=[{\"shopId\":{shopId},\"goodsId\":{goodsId}}]", ENDITEM,          
        LAST );