Fiddler grabs the package and generates the code with one click
Fiddler grabs the package and generates the code with one click
First, our requirement scenario is
After catching an interface call with Fiddler, use code to simulate the call. Generally, we write code in three steps:
- 1. Set parameters related to http request: header,method,url,cookie, etc
- 2. Set the body of post (required if it is post)
- 3 get the returned body (generally, we need to get the returned body of the interface for parsing)
How cool it would be if all the three steps of code could be generated with one click. I finally got it on the shoulders of giants!
The effect is shown in the figure below:
image
The above is a code generation demonstration of csharp using its own HttpClient. It can also be used for Java, kotlin, python, nodejs, etc
The main functions of this article are completed in FiddlerScript, mainly including 3 extensions
- Add custom right-click menu
- Add control switch
- The code gets the request context and exports the har
- Use script to complete the encapsulation of process and call
1. Add right-click menu
Click a Session in the, then right-click the menu and select generate code in the specified language, which is the most convenient to use, as shown in the following figure:
The extension method of the new right-click menu is one ContextAction + one function
For example:
public static ContextAction("C#-httpclient "," generate code ") function do1(arrSess: Session[]) { doStar(arrSess, "csharp","httpclient"); }
It means that a new level-1 menu is called code generation, and the level-2 menu is called "C#-httpclient"
The following function s are implemented by clicking the method that needs a response. The default is the Session array, because multiple can be selected.
2. Control switch
As mentioned above, there are three steps. Except that the first step is the core, the other two steps are to convert json into entity class definition, which is auxiliary. Therefore, the switch can be set to manually control whether to generate these two pieces of code
As shown below:
image
The way to add a switch is to define [a RulesOption + a corresponding received variable]
public static RulesOption("Turn off request body to code", "Generate code") var m_DisableReuqest: boolean = false;
It means that a new and menu is called generation code, and the secondary menu is called "turn off request body to code". The type is bool, because the following corresponding received variables are Boolean types!
3. Select Session to get the context of the entire request
The context includes various parameters of the request, such as url, header, method, request, response, etc
image
Fillder has an api to export har files. This har format is a standard definition proposed by Google to describe a request
Detailed documentation on har format: http://groups.google.com/group/http-archive-specification/
How to export Session to har in Fiddler
image
image
image
How to export it in code?
//This method is to export to variables. Please note that Fiddler is supported after version 4.6.2.0 var oExportOptions = FiddlerObject.createDictionary(); oExportOptions.Add("ExportToString", "true"); FiddlerApplication.DoExport("HTTPArchive v1.2", oSessions,oExportOptions, null); //This is it var sOutput: String = oExportOptions["OutputAsString"];
//This method is to export to the specified path var oExportOptions = FiddlerObject.createDictionary(); oExportOptions.Add("Filename", "Corresponding path"); FiddlerApplication.DoExport("HTTPArchive v1.2", oSessions,oExportOptions, null);
Here, I use the second method, first export the selected Session to a har file, and then use this har file as the input parameter of the next process to get the result I want!
The following is a grand introduction to the tool for generating request code based on har: httpsnippet
Open source address: https://github.com/Kong/httpsnippet
According to Kong, there is a very famous gateway. Everyone must have heard of it!
Here I have packaged this program as an exe that can run independently on windows system. You can get the download link at the end of the article.
Here, I modified the code a little and extracted the requestBody and responseBody of the har file in order to generate the corresponding POJO code as input parameters
Another tool is used here to generate the entity class POJO from json: quicktype
Open source address: https://github.com/quicktype/quicktype
It is also packaged as an exe that can run independently in windows system.
All right, put it together:
- First, generate the har file through the code
- Then httpsnippet is used to generate the code of the specified language, and the requestBody and responseBody in har are exported
- Take requestBody and responseBody as parameters respectively, and let quicktype generate entity class code
The entire code is as follows. copy to fiddler's script editor as follows:
First open the script editor:
image
Find a blank space and copy the following code:
public static RulesOption("Turn off request body to code", "Generate code")
var m_DisableReuqest: boolean = false;
public static RulesOption("Turn off the return body to code", "Generate code")
var m_DisableResponse: boolean = false;
public static ContextAction("C#-httpclient "," generate code ")
function do1(arrSess: Session[]) { doStar(arrSess, "csharp","httpclient"); }
public static ContextAction("C#-restsharp "," generate code ")
function do2(arrSess: Session[]) { doStar(arrSess, "csharp","restsharp"); }
public static ContextAction("Java-okhttp", "Generate code")
function do3(arrSess: Session[]) { doStar(arrSess, "java","okhttp"); }
public static ContextAction("Java-asynchttp", "Generate code")
function do4(arrSess: Session[]) { doStar(arrSess, "java","asynchttp"); }
public static ContextAction("Java-nethttp", "Generate code")
function do5(arrSess: Session[]) { doStar(arrSess, "java","nethttp"); }
public static ContextAction("Java-unirest", "Generate code")
function do6(arrSess: Session[]) { doStar(arrSess, "java","unirest"); }
public static ContextAction("Kotlin-okhttp", "Generate code")
function do7(arrSess: Session[]) { doStar(arrSess, "kotlin","okhttp"); }
public static ContextAction("JavaScript-xhr", "Generate code")
function do8(arrSess: Session[]) { doStar(arrSess, "javascript","xhr"); }
public static ContextAction("JavaScript-jquery", "Generate code")
function do9(arrSess: Session[]) { doStar(arrSess, "javascript","jquery"); }
public static ContextAction("JavaScript-fetch", "Generate code")
function do10(arrSess: Session[]) { doStar(arrSess, "javascript","fetch"); }
public static ContextAction("JavaScript-axios", "Generate code")
function do11(arrSess: Session[]) { doStar(arrSess, "javascript","axios"); }
public static ContextAction("Node-native", "Generate code")
function do12(arrSess: Session[]) { doStar(arrSess, "node","native"); }
public static ContextAction("Node-request", "Generate code")
function do13(arrSess: Session[]) { doStar(arrSess, "node","request"); }
public static ContextAction("Node-fetch", "Generate code")
function do14(arrSess: Session[]) { doStar(arrSess, "node","fetch"); }
public static ContextAction("Node-axios", "Generate code")
function do15(arrSess: Session[]) { doStar(arrSess, "node","axios"); }
public static ContextAction("Node-unirest", "Generate code")
function do16(arrSess: Session[]) { doStar(arrSess, "node","unirest"); }
public static ContextAction("Python3-http.client", "Generate code")
function do17(arrSess: Session[]) { doStar(arrSess, "python","python3"); }
public static ContextAction("Python-requests", "Generate code")
function do18(arrSess: Session[]) { doStar(arrSess, "python","requests"); }
public static ContextAction("ObjectiveC-nsurlsession", "Generate code")
function do19(arrSess: Session[]) { doStar(arrSess, "objc","nsurlsession"); }
public static ContextAction("Ruby-net::http", "Generate code")
function do20(arrSess: Session[]) { doStar(arrSess, "ruby","native"); }
public static ContextAction("Swift-nsurlsession", "Generate code")
function do21(arrSess: Session[]) { doStar(arrSess, "swift","nsurlsession"); }
public static ContextAction("powershell-webrequest", "Generate code")
function do22(arrSess: Session[]) { doStar(arrSess, "powershell","webrequest"); }
public static ContextAction("powershell-restmethod", "Generate code")
function do23(arrSess: Session[]) { doStar(arrSess, "powershell","restmethod"); }
public static ContextAction(