How to use url encoded data in get method? - asp.net-mvc

I am working on a restful service in asp.net MVC 4 Web API. I have a get method that getting a url encoded data as a parameter from URI. But, the thing is when I try this method from RESTClient (firefox tool), I am getting 404. And when i check the stack trace my url seems with non encoded parameter. You can see the example below:
Url format: /api/Applications/key
I am sending this:
/api/Applications/3baxYhYzpVGTrIIzs4CvGv4KIcIRHn5yqx%2F9a5PzTJxK4SWVcwi9GI6bib9pe1psk%2FdYeD0EaOdMHZpzWl%2Fbwg%3D%3D
In the response body, request url seems like this;
/api/Applications/3baxYhYzpVGTrIIzs4CvGv4KIcIRHn5yqx/9a5PzTJxK4SWVcwi9GI6bib9pe1psk/dYeD0EaOdMHZpzWl/bwg==
I am getting an error because of escape characters. I can use post but I want to go on with restful architecture.
Any advice will be appreciated.

Related

Passing a File path as a parameter in Postman

So I am trying to pass a filerepo path as a url parameter in Postman, but it keeps saying its a bad request.
Example:
http://url/api/C:\File
I have also tried the url encoded version, but postman still says bad request:
http://url/api/C%3A%5CFile

Orbeon Form HTTP Service

Does anyone know how to pass parameters to a RESTFUL webservice using the Orbeon HTTP Service?
I have a RESTFUL API at http://localhost/RESTFUL/GETADDRESS/$parameter$.
Sample of the URL is http://localhost/RESTFUL/GETADDRESS/1234
Orbeon HTTP service is unable to pass the parameter to the web service.
The Request Body is configured as <parameter/> and serialization is set to XML.
Could not use HTML Form as it adds a ? to the URL which is not correct.
Anyone has any ideas to get this working?
There is no perfect solution. But try writing the service URL as:
http://localhost/RESTFUL/GETADDRESS/{...expression here...}
where "...expression here..." should be replaced by an XPath expression pointing to the value you would like to pass. For example, if pointing to a control called foo in a section called bar, try:
http://localhost/RESTFUL/GETADDRESS/{/*/bar/foo}
I also added this RFE.

Bitbucket API OAuth not redirecting properly

I'm trying to set up Bitbucket OAuth for my site but for some reason Bitbucket is not properly redirecting back to my site. I've created an OAuth key and secret and I'm using the Guzzle OAuth plugin in my Silex application.
First I request a temporary token via the oauth/request_token endpoint. Using that token I redirect to oauth/authenticate endpoint:
$app->get(
'/auth/bitbucket',
function () use ($app) {
$client = new Client('https://bitbucket.org/api/1.0');
$oauth = new OauthPlugin(
array(
'consumer_key' => $app['bitbucket.key'],
'consumer_secret' => $app['bitbucket.secret'],
'signature_method' => 'HMAC-SHA1',
'callback' => urlencode('http://mysite.local/auth/bitbucket/callback')
)
);
$client->addSubscriber($oauth);
$response = $client->post('oauth/request_token')->send();
parse_str($response->getBody(), $result);
return $app->redirect(sprintf('https://bitbucket.org/api/1.0/oauth/authenticate?oauth_token=%s', $result['oauth_token']));
}
);
This will bring up the page on the Bitbucket site where the user can grant or deny access to their account. After I click "Grant access" Bitbucket should redirect back to the callback url that was specified earlier but instead it will append my callback url to the Bitbucket url like this:
https://bitbucket.org/api/1.0/oauth/http%3A%2F%2Fmysite.local%2Fauth%2Fbitbucket%2Fcallback?oauth_verifier=xxxxxxxxxx&oauth_token=xxxxxxxxxxxxxxxxxx
This obviously results in a Bitbucket 404 page. Does anyone have an idea why the redirect to my callback url is not working properly?
According to documentation, when requesting token from bitbucket's API, you MUST have those parameters when sending POST request to https://bitbucket.org/api/1.0/oauth/request_token:
oauth_consumer_key
oauth_nonce
oauth_signature
oauth_signature_method
oauth_timestamp
oauth_callback
Also, don't urlencode your callback URL. Replace this:
'callback' => urlencode('http://mysite.local/auth/bitbucket/callback')
With this:
'callback' => 'http://mysite.local/auth/bitbucket/callback'
When you are sending POST request, you do not need to encode any of parameters.
Indeed, as you mentioned in comment, documentation does show encoded parameters in example, as in:
https://bitbucket.org/api/1.0/oauth/request_token?oauth_version=1.0&oauth_nonce=7f2325b3c36bd49afa0a33044d7c6930&oauth_timestamp=1366243208&oauth_consumer_key=HUpRcDUduZrepL6sYJ&oauth_callback=http%3A%2F%2Flocal%3Fdump&oauth_signature_method=HMAC-SHA1&oauth_signature=qZyTwVA48RzmtCHvN9mYWmlmSVU%3D
Issue you have is not wrong documentation, but misunderstanding of POST method. Also check Wikipedia page. Unlike GET where parameters are passed in URL, POST request method stores it's data in body. That allows us to send any data type, arbitrarily long.
Data that is passed in body of request is automatically encoded as in this example (copied from Wikipedia page):
Name=Jonathan+Doe&Age=23&Formula=a+%2B+b+%3D%3D+13%25%21
Looks similar to GET method when you encode data manually, right? However, if you urlencode data in POST request you actually end up with double encoded data, which is cause of problems in your case.
I really think that some basic knowledge of HTTP methods and Internet protocols is required before playing with any API.
Also, check some HTTP traffic monitor (debugger), like free Fiddler. It will allow you to see all HTTP data that is sent from your browser, essentially enabling you to learn by own examples.
I'm not sure how your framework works, but the callback parameter may be url encoded by the framework before the request is made. Since you also url encode it, your url is url encoded twice. Bitbucket will decode it once, leaving it with a url encoded url, which won't have the scheme set (http in this case), and your browser won't know it is an absolute URL, and will thus navigate to somewhere inside Bitbucket (as you observe). Try removing the extra url encode and see if that helps.

ASP.NET WEB API 406 error:for POST request using Media format

I am very new to web api stuff:
I am getting an error
406: Not Acceptable
error message in asp.net web api rest service.
In my rest service I’m using media format for my customized XML output, to get customized output.
I’m registering my formatted media in Global.asax page.
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new mynewformat());
all my methods are post methods with typed object as parameter and parameters are accepts from body.
Whenever I try to test the service… Getting 406: Not acceptable error message.
can anyone please help me ... what could be the reason for this....???
I did notice couple of interesting points here...
If I’m commenting below line then I’m getting 200 (OK) status code (which is fine.)... but format is not applying to output.
GlobalConfiguration.Configuration.Formatters.Clear();
If i'm removing parameters in my service method.. Then its working
fine..
I request everyone.. Please guide me what could be the reason/work around/solution/fix..for this issue.
Note:I don't want accept parameters from URI so i made it to accept from frombody only.
Thanks.
There is a lot more to implementing a custom format than just adding it to the configuration formatters. It starts with having to change the media-type header to a new custom type of your choosing (like "application/myNewFormat") for all requests, for the client. On the back end, you have to implement a new MediaTypeFormatter that can handle the serialization. This involves a bit more of code.
A good example of this resides here, it can easily be stripped to boiler-plate code:
http://www.codeproject.com/Articles/559378/Implementing-Custom-Media-Formatters-in-ASP-NET-We

How can I Get the RAW URL from a request in Rails?

I'm having trouble debugging a client, and I'm trying to get the raw URL on the server which is in rails. I'm wondering how I can dump the raw URL/http message that is hitting rails.
If found query_string, which works okay for gets. But if a user does a post, I can't seem to find the raw string anywhere.
All I can find is post-parsed parameters in hashes vs raw URLs.
Help?
request.url
Will give the current url.
See other answers:
How do I get the current absolute URL in Ruby on Rails?

Resources