C# Query String and x-www-form-urlencoded - c#

I have an ashx processing POST of a form on my website.
Most of the time, I am sent data with content-type x-www-form-urlencoded.
If my user is under IE8/9, I have to use XDomainRequest object instead of the usual XmlHttpRequest.
In the first case, I use context.Request.Form["..."] to read the datas, but HttpRequest.Form is only populated if content-type is x-www-form-urlencoded.
In the IE8/9, XDomainRequest only support plain/text content-type, hence I'd have to use HttpRequest.Querystring to get the datas.
My question is : Is Querystring populated when the content type is x-www-form-urlencoded ?

Related

POST a multiple fragment JSON using Postman with application/json header

I have the following JSON
{"name":"tester1","type":"frontend"}
{"name":"tester2","type":"midlleware"}
{"name":"tester3","type":"backend"}
When I paste the above into Postman with an application/JSON, only the 1st row/JSON will be passed to the function because of the lack of commas between each row. Note, I cannot change the incoming JSON format nor the Content-Type (application/JSON). Also note this is a valid multipart JSON
See here
My API method is as follows:
public async Task<HttpResponseMessage> ProcessJson([FromBody] string _incomingJSON)
Note: I can change my signature ([FromBody] string _incomingJSON)
_incomingJSON is null if I use application/JSON but populated if I use text/plain (which I can't use)
Is there any way I can handle this format with application/JSON and not get _incomingJSON as null?
Thanks in advance
You should send with Postman in 3 rows like this:

Pass model via "redirect to action" visible to URL String in Browser

The thing is I'm passing some data in model from first action to second action of same controller
but the data in model is visible in URL query string.
This is how it is visible to Query string
Now
1 - is there any solution to hide that data in query string Other than using Temp Data
2 - and why redirection to action is happening from client ??
3 - It seems to be passing data via Get method in redirection, is there any way I can pass data in Body (i.e. Post).
The MVC RedirectToAction method simply sends the client (browser, in this case) a HTTP header called "Location" in the response, and sets the status code to 302 "Found", to indicate a suggested redirect. That header contains the URL of the view you specified, and puts any model data onto the querystring.
The client can choose whether to visit that URL or not. Browsers generally follow the link automatically, because it's user-friendly to do so. Other HTTP clients may not do so. That's how it works - this is general to the whole web, not just MVC. The redirect is always done as a GET, it's not possible to redirect via any other HTTP method.
If you want to just display the view without a redirect, then use
return View("addLeaveEntitlement", result.Model)
Then there's just one request, there's no redirect, and the URL does not change at all (not even to the regular URL of the "add" view - this may or may not be desirable).

How to receive a POST, using C#, without knowing the content and throw int to a variable?

So, I configured an external (3rd party) service to send me data through push requests. There aren't many documentation on the format of this data, they just say to point for a URL that accepts POST.
So, I want to capture this message and add to a variable that I can add to a temporary database, and analyse it to create a proper treatment later.
All examples I saw show how to send and capture the response, but I just want to capture a random message that I don't know.
Any Hints?
Regardless of the format of the request they send to your application, everything will be contained within the HttpRequest object (which, if you're just talking about a Page or something similar, would/should be built-in as the .Request property of that page).
If the request has form values, the .FormData property on that object will be a NameValueCollection of those values. If it has something in the POST body, the .InputStream property on that object will contain that data. Files, headers, query string values, etc. will all be on that object.
You can copy whatever you like from that object into your data, inspect what you get, tinker to more specifically target the values they send you, etc. Without knowing the format of the data they're going to send you, anything more specific will require inspecting what they send you and reverse-engineering its format from there. But all the data will be in that HttpRequest object.
You can use the InputStream property of the Request object. This will give you the raw data of the HTTP request.
if (Request.RequestType == "POST")
{
using (StreamReader reader = new StreamReader(Request.InputStream))
{
var wholeRequest = reader.ReadToEnd();
}
}
If you just want the request body, you can use Request.Form.ToString().
if (Request.RequestType == "POST")
{
var wholeForm = Request.Form.ToString();
}
I would not use the .FormData NameValueCollection unless you know they will be using a content-type of application/x-www-form-urlencoded multipart/form-data. Other content types will not properly populate that collection.

Translating this to a HTTP POST in C#

I am currently experimenting with the HTTP request. I have successfully managed to do get requests and I have read on doing post request with HTTP request.
Now I am trying to work with the yahoo API and in order to use the Yahoo api it states that at
The Message Management API can be used to send a message to another
Yahoo! Messenger contact. The API is very simple to use, as shown
here. Note that the contact that the message is sent to is part of the
URI, using the following format:<server>/v1/message/<network>/<contactID>
POST /v1/message/yahoo/targetYahooId?sid=msgrsessionid
Host: rcore1.messenger.yahooapis.com
Authorization: < Standard OAuth credentials >
Content-Type: application/json;charset=utf-8
Content-Length: 25
{
"message" : "Hey there"
}
Now I have an OAuth string which I obtained from get using the HttpWebRequest object.
The string is something like this
oauth_token=A%3Dvh....aRg--&oauth_token_secret=bd46a....c9239&oauth_expires_in=3600&oauth_session_handle=ALtT.....3J1N4Zg--&oauth_authorization_expires_in=784964948&xoauth_yahoo_guid=TUSKED5...NCIA
UPDATE
Now my question are as follows :
1- If I am using WebRequest object in C# what would my URI look like
2- I understand that it requires a JSON type object. How do i even know what OAuth parameters are ?
One thing you'll need to change is the content type:
request.ContentType = "application/json;charset=utf-8";
And of course, the url.
you need to change the url on the line with the url in it
you need to change the content-type line
you need to make the payload into a json string then convert it to a byte array (byteArray in the sample)
either assemble the json by hand "{ foo:'bar'}" etc or use json.net
and set the content-length
Looks like it's expecting a JSON object for the request body. Depending on the version of .NET you're using, you can either use a Javascript serializer as shown here (https://stackoverflow.com/a/7003815/939080) or JSON.NET (http://james.newtonking.com/projects/json-net.aspx) to convert your form collection into JSON output.
You are asking an open-ended question that would require people to write a bunch of code for you if you want a specific and complete answer. As others have pointed out, there are several issues that you'd need to deal with:
The JSON payload, which would be a straightforward matter of putting the JSON string in the request body via the byteArray used in the code sample.
The content type, which you would need to change as described by jrummell.
The OAuth credentials, which is a kettle of fish you'll need to read about, understand, and acquire a library for. Here's a good place to start looking for a library.

how to set up request body with httpWebRequest

I want using httpWebRequest to "POST" data to a website. So i used firebug analyzing what really send to server. First step, i use browser to browsing www.mytargetURL.net, second, turn on firebug, after that, i fill all form data and click the submit button (it's mean POST data to the server). So, I watching firebug and there was a lot of Parameters in request body part. Something like:
param1=
param2=
param3=default_value1
param4=default_value2
param5=value_I_set_byhand1
param6=value_I_set_byhand2
The question is: I should set up the request body of httpWebRequest obj with all the parameters i saw in firebug parameter table (it's mean all 6 parameters) or just parameters which has value (parameter 3-6) or just parameters i have filled in the submit form(just param5 and param6)?
Thanks you for all supports
You create the HttpWebRequest object, get the request stream, and write your parameters to it. The example at HttpRequest.GetRequestStream should point you in the right direction.

Categories

Resources