54

I am using Mailgun API. There is a section that I need to provide a URL to them, then they are going to HTTP Post some data to me.

I provide this URL (http://test.com/MailGun/Webhook.aspx) to Mailgun, so they can Post data. I have a list of parameter names that they are sending like (recipient,domain, ip,...).

I am not sure how get that posted data in my page. In Webhook.aspx page I tried some code as follows but all of them are empty.

 lblrecipient.text= Request.Form["recipient"];

 lblip.Text= Request.Params["ip"];

 lbldomain.Text = Request.QueryString["domain"];

Not sure what to try to get the posted data?

Alma
  • 3,780
  • 11
  • 42
  • 78

9 Answers9

54

This code will list out all the form variables that are being sent in a POST. This way you can see if you have the proper names of the post values.

string[] keys = Request.Form.AllKeys;
for (int i= 0; i < keys.Length; i++) 
{
   Response.Write(keys[i] + ": " + Request.Form[keys[i]] + "<br>");
}
gunwin
  • 4,578
  • 5
  • 37
  • 59
James Lawruk
  • 30,112
  • 19
  • 130
  • 137
  • 6
    @eloycm Yes, fiddler would be fine locally, however it sounds like the POST requests are coming into the server from an external source. – James Lawruk Nov 22 '13 at 19:55
  • This answer is pretty good -- except for the writing the response part -- it does Alma no good if Mailgun get's a list of variables. – Gerard ONeill Jan 31 '17 at 22:29
47

This code reads the raw input stream from the HTTP request. Use this if the data isn't available in Request.Form or other model bindings or if you need access to the bytes/text as it comes.

using(var reader = new StreamReader(Request.InputStream))
    content = reader.ReadToEnd();
Fred Mauroy
  • 1,219
  • 1
  • 11
  • 10
10

You can simply use Request["recipient"] to "read the HTTP values sent by a client during a Web request"

To access data from the QueryString, Form, Cookies, or ServerVariables collections, you can write Request["key"]

Source: MSDN

Update: Summarizing conversation

In order to view the values that MailGun is posting to your site you will need to read them from the web request that MailGun is making, record them somewhere and then display them on your page.

You should have one endpoint where MailGun will send the POST values to and another page that you use to view the recorded values.

It appears that right now you have one page. So when you view this page, and you read the Request values, you are reading the values from YOUR request, not MailGun.

Hasan Fathi
  • 5,610
  • 4
  • 42
  • 60
Andy T
  • 10,223
  • 5
  • 53
  • 95
  • this Request["recipient"] is empty as well. – Alma Nov 22 '13 at 18:23
  • the problem is he doesn't know the real names of the params sent to the page – eloycm Nov 22 '13 at 18:27
  • In that case @James' solution would be helpful – Andy T Nov 22 '13 at 18:28
  • @eloycm Actually I know the name of the params sent to the page – Alma Nov 22 '13 at 21:43
  • From the sounds of it, you have an external service that is sending you data. You probably have a button on the page which causes an event on MailGun. MailGun then hits your URL with some data. Please let me know if this is close to what is happening. – Andy T Nov 22 '13 at 22:06
  • @QuetiM.Porta Actually I don't have any button in my page. Mailgun Automatically posting data the URL I provided to them every day. I am trying to get this Posted data in page load. – Alma Nov 22 '13 at 22:17
  • Ok, so you want to display the posted values from a request that MailGun is making. The problem is that the request that MailGun is making has nothing to do with the request you are making to view the web page. Meaning that you can be viewing the page and MailGun can be posting requests, but you will not see them. What you need to do is record the values when MailGun makes a post, then when you view the page, read those values into the screen. – Andy T Nov 22 '13 at 22:34
  • @QuetiM.Porta how record the values? – Alma Nov 22 '13 at 22:40
  • You can record to anything that can persist your data: database, file or maybe just send an email for each post. Basically, anything that will allow you to later view what was posted. Here is a getting started with Entity Framework: http://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet-getting-started-part-1 – Andy T Nov 22 '13 at 22:46
  • @QuetiM.Porta I don't think it is something to do with the Entity framework. Data can be save in database in simple way. But the problem is when data is not existed how can save them. So first I have to figure out why I am not receiving the posted data. But thanks for your help anyway. – Alma Nov 22 '13 at 22:55
3

You are missing a step. You need to log / store the values on your server (mailgun is a client). Then you need to retrieve those values on your server (your pc with your web browser will be a client). These will be two totally different aspx files (or the same one with different parameters).

aspx page 1 (the one that mailgun has):

var val = Request.Form["recipient"];
var file = new File(filename);
file.write(val);
close(file);

aspx page 2:

var contents = "";
if (File.exists(filename))
  var file = File.open(filename);
  contents = file.readtoend();
  file.close()

Request.write(contents);
Gerard ONeill
  • 3,914
  • 39
  • 25
1

Use this:

    public void ShowAllPostBackData()
    {
        if (IsPostBack)
        {
            string[] keys = Request.Form.AllKeys;
            Literal ctlAllPostbackData = new Literal();
            ctlAllPostbackData.Text = "<div class='well well-lg' style='border:1px solid black;z-index:99999;position:absolute;'><h3>All postback data:</h3><br />";
            for (int i = 0; i < keys.Length; i++)
            {
                ctlAllPostbackData.Text += "<b>" + keys[i] + "</b>: " + Request[keys[i]] + "<br />";
            }
            ctlAllPostbackData.Text += "</div>";
            this.Controls.Add(ctlAllPostbackData);
        }
    }
Tone Škoda
  • 1,463
  • 16
  • 20
0

In the web browser, open up developer console (F12 in Chrome and IE), then open network tab and watch the request and response data. Another option - use Fiddler (http://fiddler2.com/).

When you get to see the POST request as it is being sent to your page, look into query string and headers. You will see whether your data comes in query string or as form - or maybe it is not being sent to your page at all.

UPDATE: sorry, had to look at MailGun APIs first, they do not go through your browser, requests come directly from their server. You'll have to debug and examine all members of Request.Params when you get the POST from MailGun.

Roman Polunin
  • 53
  • 3
  • 12
  • I used F12 and Network tab, I am not seeing any on that parameters here where should look for them? – Alma Nov 22 '13 at 18:14
  • I don't know about explorer, for sure is not the best tool for web development, I don't see the post parameters there either (I rarely use IE anyway). another good option is firebug on firefox, there you get all the post parameter passed to the page – eloycm Nov 22 '13 at 18:26
  • Thanks @eloycm, I am installing firefox now. – Alma Nov 22 '13 at 18:32
  • @eloycm, in firefox, I can see response header and request header, is it the place that I should see the parameters that are posting to this page? I am not seeing any of parameters that I have name of them here :( – Alma Nov 22 '13 at 18:35
  • in firefox if you expand the request (clicking on the little cross on the left) you will see headers and then the post tabs, there in the post tab you should see a list of parameters – eloycm Nov 22 '13 at 18:38
  • you also have to install firebug, it's the firefox developer extension, you turn on firebug, then look for the network panel, then you will see a list of the network activity that you can expand in order to see details like the post... – eloycm Nov 22 '13 at 18:47
  • I can see Request headers, Reponse Headers, also I have these panels: Headers, Cookies, Params,response,Timing. But I am not seeing Post, Param tab also is empty. – Alma Nov 22 '13 at 18:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41724/discussion-between-eloycm-and-nikoo-m) – eloycm Nov 22 '13 at 19:04
  • @eloycm, I have all the panels shown in the video except Post. – Alma Nov 22 '13 at 19:34
0

Try this

string[] keys = Request.Form.AllKeys;
var value = "";
for (int i= 0; i < keys.Length; i++) 
{
   // here you get the name eg test[0].quantity
   // keys[i];
   // to get the value you use
   value = Request.Form[keys[i]];
}
Ananda G
  • 2,389
  • 23
  • 39
0

In my case because I assigned the post data to the header, this is how I get it:

protected void Page_Load(object sender, EventArgs e){
    ...
    postValue = Request.Headers["Key"];

This is how I attached the value and key to the POST:

var request = new NSMutableUrlRequest(url){
    HttpMethod = "POST", 
    Headers = NSDictionary.FromObjectAndKey(FromObject(value), FromObject("key"))
};
webView.LoadRequest(request);
Mr.K
  • 559
  • 4
  • 9
0

You can try to check the 'Request.Form.Keys'. If it will not works well, you can use 'request.inputStream' to get the soap string which will tell you all the request keys.

viking
  • 11
  • 2
  • These two answers are already covered in previous answers to this question. When answering be sure to provide new answers, ideally with an example of how to use it in the context of the original question. – siva.k Aug 27 '21 at 02:03