Restful PUT acting as GET when testing - c#

So I've been working on an api that, using RESTful, allows the user to get data from a database. Pretty simple, URL is along the lines of local:port/projects/[id of project]. The api returns some xml with 4 or 5 results.
What I'm having trouble with is PUT. As far as I understand it, I should use the same url, but use the PUT request method, and include the data that I want sent in a parameter. The problem seems to be that when I run the PUT, it just returns the same data as a GET.
I'm using the following site to test this: wst dot mytechlabs dot com (won't let me post two links here;?)
The code for my controller is located here: http://pastebin.com/3HXXR4YY
Thanks in advance, I'll monitor this, so let me know if I forgot any info that would help.

Related

How do I send a POST Request to Google Translate Api using a C# WebClient?

I googled now for almost an hour and can't find anything that would help me out. I'm a beginner programer and took the course over from TeamTreeHouse with Serialization in C#. Here I also learned how to use the WebClient.
Now I thought a good practice Project would be to make a Translate app that sends to google the user input in a POST Request and Google returns the answer as Json which I deserialize.
Problem is I read through the documentation of the api but I'm so confused of what I should send exactly to google and really how to do this?
I know the method webclient.Headers.Add(arguments here) , but I really don't know what else it needs.
You can find an example over here:
https://cloud.google.com/translate/docs/translating-text
https://translation.googleapis.com/language/translate/v2 Three query
parameters are required with each translation request:
Target language: Use the target parameter to specify the language you
want to translate into. Source text string: Use the q parameter to
specify each text string to translate. API key: Use the key parameter
to identify your application. If you are using OAuth 2.0 service
account credentials (recommended), do not supply this parameter.
So problem is there is no placeholder in that example URL where I could put my api key + soure text string an Target language.
So what exactly should I send to google so it knows what I want and returns me the JSON file?
Maybe anyone could help me out. And I know there is an official Library for exactly this but I want to practice serialization and using web scraping with the WebClient class so I want to do it like this.
You specifically asked about a POST.
You can use this url:
https://translation.googleapis.com/language/translate/v2?key=MY_KEY
Of course, replace MY_KEY with your key.
Add a header for the content type:
application/json; charset=utf-8
Format your text and target language as JSON and write it to your request stream:
{"q":"Team work is a major progress maker at this location, it appears that everyone is willing to help when they can.","target":"fr"}
The documentation here: https://cloud.google.com/translate/docs/reference/translate#body.QUERY_PARAMETERS
says use query parameters q, target, key, etc.
So your URL should be something like https://translation.googleapis.com/language/translate/v2?key=[yourAPIkey]&target=language

c# WebRequest not returning the required page, possibly a header issue

I'm writing an app to ensure my website is always up to date with our suppliers products. I can get the categories but not the subs.
Basically a webrequest on "xxxx/products/8-propagation/?sub-category=96" always returns "xxxx/products/8-propagation/". I have used console on firefox to see what headers are sent when browsing, I didn't see anything particular but still emulated.
Is there any way to retrieve php requests from URL's or is this something server side only?
I have tried numerous ways of doing this, all the same result.
Show us your server side code. I think, this is problem with your routing in controller.

Making restful calls to asp.net web api

I am currently following this tutorial to create a simple REST service using Web Api. Note this is my first time doing something like this and I am just trying to learn more.
http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations
I have followed all the instructions and have it successfully running on my localhost. I understand that in this tutorial the URI for all my GET requests look something like:
localhostapi/products/id
And I understand that, and how to perform simple GET requests in the URI and see it actually occuring using my developer tools in my browser.
Now my question is... How do I make POST/DELETE/PUT requests and actually see what they are doing? The guide wasn't too clear, do I pass in parameters into the URI? Does the URI change when I want anything but a GET request? This text here seems to explain it but I do not understand:
The method takes a parameter of type Product. In Web API, parameters with complex types are deserialized from the request body. Therefore, we expect the client to send a serialized representation of a product object, in either XML or JSON format.
It's quite easy to make POST, PUT, DELETE requests. You just need to install Fiddler at http://www.telerik.com/download/fiddler
Next, install and run it. Go to the Composer tab on the right hand side. Next, put your local host URL, and the request method, and other data like the screenshot below
You can write unit tests, like
[TestMethod]
public void GetAllProducts_ShouldReturnAllProducts()
{
var testProducts = GetTestProducts();
var controller = new SimpleProductController(testProducts);
var result = controller.GetAllProducts() as List<Product>;
Assert.AreEqual(testProducts.Count, result.Count);
}
This link also This one may help.
more:
How to call ASP .NET MVC WebAPI 2 method properly
Sending C# object to webapi controller
You can set a breakpoint on your controller methods that handle the post/delete/put.
Same thing in your browser at the point where you call the post/delete/put (presumably in a jquery request)
You can also unit test your controller methods:
http://www.asp.net/mvc/tutorials/older-versions/unit-testing/creating-unit-tests-for-asp-net-mvc-applications-cs

Getting data from a webpage

I have an idea for an App that would really help me out in work but I'm not sure if it's possible.
I want to run a C# desktop application that will ask for a value. When a value is supplied, the application will open a browswer, go to a webpage and add the value into a form on an online website. The form is then submitted and a new page is loaded that contains a table of results. I then want to extract the table of results from the page source and write code to parse the result values.
It is not important that the user see's this happen in an actual browser. In other words if there's a way to do it by reading HTTP requests then thats great.
The biggest problem I have is getting the values into the form and then retrieving the page source after the form is submitted and the next page loads.
Any help really appreciated.
Thanks
Provided that you're only using this in a legal context:
Usually, web forms are sent via POST request to the web server, specifically some script that handles it. You can look at the HTML code for the form's page and find out the destination for the form (form's action).
You can then use a HttpWebRequest in C# to "pretend you are the form", sending a POST request with all the required parameters (adding them to the HTTP header).
As a result you will get the source code of the destination page as it would be sent to the browser. You can parse this.
This is definitely possible and you don't need to use an actual web browser for this. You can simply use a System.Net.WebClient to send your HTTP request and get an HTTP response.
I suggest to use wireshark (or you can use Firefox + Firebug) it allows you to see HTTP requests and responses. By looking at the HTTP traffic you can see exactly how you should pass your HTTP request and which parameters you should be setting.
You don't need to involve the browser with this. WebClient should do all that you require. You'll need to see what's actually being posted when you submit the form with the browser, and then you should be able to make a POST request using the WebClient and retrieve the resulting page as a string.
The docs for the WebClient constructor have a nice example.
See e.g. this question for some pointers on at least the data retrieval side. You're going to know a lot more about the http protocol before you're done with this...
Why would you do this through web pages if you don't even want the user to do anything?
Web pages are purely for interaction with users, if you simply want data transfer, use WCF.
#Brian using Wireshark will result in a very angry network manager, make sure you are actually allowed to use it.

Describing ASMX web service WebMethod parameters

This seems like a simple question, but I haven't been able to find the answer online via many Google searches. I have a C# web service and, when I visit its ASMX page in the browser, for a particular method it always has the following:
"The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values."
Likewise for SOAP 1.2 and HTTP POST. What I want to know is how I replace the placeholders shown, which are things like:
<myParameter>string</myParameter>
Where 'string' is the placeholder. And in the response:
<xsd:schema>schema</xsd:schema>xml
Where 'schema' and 'xml' are the placeholders. I've been using another web service that has these placeholders filled out with example values for the parameters and responses, and I would like to define such examples for my own web methods, too. I was able to describe the entire method with the following:
[WebMethod(Description="Does awesome things.")]
But I have not found such a thing for the individual parameters of a web method.
By default DefaultWsdlHelpGenerator.aspx is called to generate the "help" page.
You can set another (or modified) template with the wsdlHelpGenerator Element in your web.config file.
Why would you want to do that? That page you see in the browser when hitting your asmx is just giving sample requests and reponses. If you want to get data using those examples, replace the placholder values in the request with what you are querying from the service, and POST to it...
Edit: I mean, if you really need to replace those placholder values, write code in your service to determine when someone does a GET (implying viewing from a browser), and play with the response, changing the placeholder values to whatever you require.
You can't do it. If the help page (which is what you're describing) does not have an input box for a particular parameter, then it means it doesn't know how to do that.
You should not pay too much attention to those pages in any case. They go away with WCF.
They were never of very much use anyway, except for the simplest web services. They were a way to get people into the web service game way back in the beginning when there were no tools to help you test a web service. Use soapUI instead.
BTW, also see Microsoft: ASMX Web Services are a “Legacy Technology” for why it makes good sense to ignore ASMX-only pages.

Categories

Resources