Copying, modifying and sending request POST

1 Answer 382 Views
Fiddler Classic
Max
Top achievements
Rank 1
Iron
Max asked on 26 Oct 2021, 06:04 PM

hi everyone, sorry for my English)))

 

I have been trying to solve my problem for a long time, but unfortunately so far without success ((((
I have a POST request of a certain type, I need to change it and send it to another address, and the original request should go further to its address. I'm trying to work with the exchange. More to the point, I have a request like this :

 

I need to send this request after the original one

https://fapi/binance.com/fapi/v1/order?symbol=KEEPUSDT&side=BUY&type=MARKET&quantity=100&timeInForce=GTC&recvWindow=5000&timestamp=1591702613943&signature= 3c661234138461fcc7a7d8746c6558c9842d4e10870d2ecbedf7777cad694af9
    

 

In addition, the public API is passed in the header

I tried to do all this work in the onBeforeRequest method

 

 static function OnBeforeRequest(oSession: Session) {
       
		
	if (oSession.RequestMethod == "POST" &&  oSession.uriContains("order") ) {
       
         oSession.utilDecodeResponse()
        var strBody=oSession.GetRequestBodyAsString();
 
    
           // timestamp, time ms
            
            var timestampSTART= oSession.url.IndexOf("timestamp=") + 10; 
            
            var timestampEND= oSession.url.IndexOf("&", timestampSTART);   
           
            var timestamp = oSession.url.Substring(timestampSTART, 13);
            
           
            // SYMBOL
            
            var symbolStart = oSession.url.IndexOf("symbol=")+7;
           
            var symbolend = oSession.url.IndexOf("BTC", symbolStart)+3;
       
            var symbol = oSession.url.Substring(symbolStart, symbolend-symbolStart);
            
            
           //  Signature (timestamp+SecretAPIKey=Hmac Sha256)   theoretically, it can be taken from the original request, but it is more reliable to make your own 
           var signStart = oSession.url.IndexOf("signature=")+10;
                         
           var sign = oSession.url.Substring(signStart);
            
           
            //PRICE
            
         var PriceStart = oSession.url.IndexOf("price=")+6;
         var PriceEND = oSession.url.IndexOf("&", PriceStart);
         var priceStr = oSession.url.Substring(PriceStart, PriceEND-PriceStart);
         var price = parseFloat(priceStr);
            
                                    
            // Quantity
            
            var quantity = 50/ price*63000;
      
            var apiBIN =  "https://fapi.binance.com/fapi/v1/order?" ;
          
           
         //    var result = apiBIN+"symbol="+symbol+"&side=BUY&type=MARKET&quantity="+ quantity+"&timeInForce=GTC&recvWindow=5000&timestamp="+timestamp+"&signature="+sign;
                
         //    oSession.utilSetRequestBody(result)
      
         //   FiddlerApplication.oProxy.SendRequest(oSession.RequestHeaders, oSession.requestBodyBytes, null);
                
         

I have selected the necessary parameters from the request, but I do not understand how to send it in any way, and I also lose the header with the API key. 
Separately, I want to note the "Signature" parameter, which is created using the Hmac Sha256 algorithm from the secret API key and time, which I also can't figure out how to describe it in the code.

I would be very grateful for any help, with the possibility of some payment for substantial help.

Nick Iliev
Telerik team
commented on 27 Oct 2021, 08:54 AM

You can make the request modification in onBeforeRequest, for example, something like:

if (oSession.HostnameIs("your-host-name-here")) {
    oSession.oRequest["NewHeaderName"] = "New header value";
    // oSession.oRequest.headers.Remove("Cookie");
}

With the above, any request to "your-host-name-here" will be executed with the modifications made through the codebase.

Note that utilSetRequestBody is a method that sets the body of a request and not its URL. If you are trying to change query parameters you need to change the URL (through oSession.url). Basically, it looks like that you would like to change the URL, so that would mean that you will end up with code similar to that:

if (oSession.url=="your-api-here/?queryParam1=value1") {
  oSession.url = "your-api-here/?queryParam1=newValue222";
}

As those changes will be made in onBeforeRequest, there is no need to call oProxy.SendRequest as this will make a new request (if you want to repeat the request, you should call it in the AfterSessionComplete event)

Max
Top achievements
Rank 1
Iron
commented on 27 Oct 2021, 10:44 AM | edited

after many attempts, I almost succeeded!!! I did all the work in the before Response method, and I still have 2 main questions. 
How to get a timestamp like this -"1591702613943".
And the second question is how to encrypt the query string with the key.  
There is such a class System.Security.Cryptography.HMACSHA256(), but I don't understand how I can use it correctly. 

I need to encrypt the request body using my secret key, how do I do this?

1 Answer, 1 is accepted

Sort by
0
Accepted
Max
Top achievements
Rank 1
Iron
answered on 31 Oct 2021, 11:34 AM | edited on 31 Oct 2021, 11:35 AM
 if (oSession.RequestMethod == "POST" ) {
            
         
                  
      // easy sample chande request                
           oSession.utilReplaceInRequest("api/v3/","fapi/v1/")
            
   
            
            var strBody=oSession.GetRequestBodyAsString();
            
            
      //some code for change request
          
            
            var strQuery = ""
                     
            
       //Send request     
           FiddlerApplication.oProxy.SendRequest(oSession.RequestHeaders, strQuery , null);
            
                     
            
            }
Tags
Fiddler Classic
Asked by
Max
Top achievements
Rank 1
Iron
Answers by
Max
Top achievements
Rank 1
Iron
Share this question
or