justcoding121 / titanium-web-proxy

A cross-platform asynchronous HTTP(S) proxy server in C#.
MIT License
1.93k stars 618 forks source link

Titanium Web Proxy - Can't modify request body #794

Open nikolapejic95 opened 4 years ago

nikolapejic95 commented 4 years ago

Hi everyone, I'm having a problem with modifying the requests before they reach the browser. I've posted the question on stack overflow, please check it out:

https://stackoverflow.com/questions/63508922/titanium-web-proxy-cant-modify-request-body

The solution might be obvious, but I don't seem to be able to find out how to fix it. Any help would be appreciated.

honfika commented 4 years ago

Is the onRequest called for your website?

The code on stackoverflow looks strange. I doubt that it is from the example from here. Why do you use Task.Run instead of simply awaiting that call? I think you should not call the GetResponseBody from the onRequest handler, since the request will be sent to the server only after this call...so you won't be able to get the response here.

nikolapejic95 commented 4 years ago

HI @honfika

thank you for the fast response!

Oh my God... I can't believe I've missed that :|

Putting modifications under onResponse works like a charm, thank you for the help!

By the way, I'm using Task.Run since await gives me "Cannot await void" for some reason. I'm using .NET 4.5

honfika commented 4 years ago

Yeah, because it is a symchronous method, in it unnecessary to wrap it in a task and await it... it just makes your code slower.

bbhxwl commented 4 years ago

help

tungpcco commented 3 years ago

at the onRequest function of ProxyTestController. you can add if (e.HttpClient.Request.Method == "GET" || e.HttpClient.Request.Method == "POST") { var bodyBytes = await e.GetRequestBody(); e.SetRequestBody(bodyBytes);

            var body = e.GetRequestBodyAsString();

            //e.SetRequestBodyString(body.Result);
            if (body.Result.Contains("username")) {
                string _aloc = body.Result.Replace("xxx", "yyy");
                e.SetRequestBodyString(_aloc);
            }

        }