locked
Get http posted values from a different site using RESTful Web Service RRS feed

  • Question

  • User1513064915 posted

    Hey, this is my first web service.  I have been reading about them to gain some knowledge.  Went through a few samples of web services.  I'm trying to create a RESTful web service to retrieve values from a http post from a different site.  The user from the different site knows I will be getting these values from his http post.  So I want to create a RESTful web service to achieve this.  Any info is appreciated.

    Wednesday, February 27, 2013 1:45 PM

Answers

  • User220959680 posted

    I'm trying to create a RESTful web service to retrieve values from a http post from a different site.  The user from the different site knows I will be getting these values from his http post. 

    Can the service be used in another website ? so that the user POST the data to your service? Is this is what above description?

    To order to retreive data the HTTP verb that is used in RESTFul is GET , POST is used to create a new resource/record.

    ASP.NET Web API is simple and light weight framework to build RESTFul service.

    Refer http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations

    Implement the service as explained in above reference, provide the service URI to the user to post data. Note that you need to implement POST method to create/store the data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 27, 2013 8:25 PM
  • User220959680 posted

    I'm not getting any exceptions.  But when I run it....only the get is working.  The Post doesn't fire.  Not sure if I have a setting wrong or what.

    Go through this video tutorial first http://www.asp.net/web-api/videos/getting-started/delete-and-update 

    * Ignore the video title. It walks through POST to create a new record. 

    Then investigate your source or the testing process.

    when it is still the same, please explain how you are testing in detail. Also with your implementation (source code).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 5, 2013 10:24 AM

All replies

  • User220959680 posted

    I'm trying to create a RESTful web service to retrieve values from a http post from a different site.  The user from the different site knows I will be getting these values from his http post. 

    Can the service be used in another website ? so that the user POST the data to your service? Is this is what above description?

    To order to retreive data the HTTP verb that is used in RESTFul is GET , POST is used to create a new resource/record.

    ASP.NET Web API is simple and light weight framework to build RESTFul service.

    Refer http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations

    Implement the service as explained in above reference, provide the service URI to the user to post data. Note that you need to implement POST method to create/store the data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 27, 2013 8:25 PM
  • User1513064915 posted

    Correct.  I will be supplying the user of a vendor site the url.....so the user will POST the data to my service. 

    I understand that I would use GET.   I have went through the Tutorial you supplied and have it working but not sure how I would call the data from the Users POST since the tutorial already supplies the data.  Now once I have the data, I need to display the data back to the user to conirm successful transmission of data.  So I think I am close but trying to test this out on my end to confirm it is working.

    Thursday, February 28, 2013 11:20 AM
  • User220959680 posted

    Service will implement the POST method, which is consumed by the client to create a record. After successful creating of record using POST method your service returns HTTP status code 200. Being said that it is always feasible to return a confirmation message or similar as you wish. But the standard practice is reutning HTTP status code (please refer below copied article snippet).

    In order to read recently created record using POST method by the client, you can implement a GET method that returns a single record. Refer http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations

    Let me know further help.

    copied from the Web API tutorial article

    Creating a Resource

    Next, we'll add a method to the ProductsController class to create a new product. Here is a simple implementation of the method:

    // Not the final implementation!
    public Product PostProduct(Product item)
    {
        item
    = repository.Add(item);
       
    return item;
    }

     Note two things about this method:

    • The method name starts with "Post...". To create a new product, the client sends an HTTP POST request.
    • 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. 

    This implementation will work, but it is not quite complete. Ideally, we would like the HTTP response to include the following:

    • Response code: By default, the Web API framework sets the response status code to 200 (OK). But according to the HTTP/1.1 protocol, when a POST request results in the creation of a resource, the server should reply with status 201 (Created).
    • Location: When the server creates a resource, it should include the URI of the new resource in the Location header of the response.

    ASP.NET Web API makes it easy to manipulate the HTTP response message. Here is the improved implementation:

    public HttpResponseMessage PostProduct(Product item)
    {
        item
    = repository.Add(item);
       
    var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);

       
    string uri = Url.Link("DefaultApi", new { id = item.Id });
        response
    .Headers.Location = new Uri(uri);
       
    return response;
    Thursday, February 28, 2013 11:38 AM
  • User1513064915 posted

    Ok.  I'm almost there.   So how would I check to make sure my code works.

    Thursday, February 28, 2013 2:45 PM
  • User220959680 posted

    Read the section 'Calling the Web API from the Browser' at http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    Video tutorials

    http://www.asp.net/web-api/videos

    Thursday, February 28, 2013 3:29 PM
  • User1513064915 posted

    The tutorial nor the videos assisted me with the test I want to do.  I want to test...me posting data from a separate application to the web service.  Then the web service displaying the data back.  I have went through the Products tutorials....which worked.  So I then added my own information and files for a web service to get data.  I don't want to provide the data within the web service which is what the products tutorial is doing.  I have been trying to get this work work by changing the url to have http://localhost:xxxx/api/MyValues but won't work.  Even trying the default...it still renders the products page after I removed all of the products files and cleared the cache.

    Thursday, February 28, 2013 5:20 PM
  • User220959680 posted

    me posting data from a separate application to the web service. 

    Testing POST method in web browser is same as providing the service URI to the user to use in web site, assuming that it is itnernal (no security implications).

    Then the web service displaying the data back.

    This is demonstrated in the article referred in earlier response. check the below it returns the response after successful creation of the product.

    //ASP.NET Web API makes it easy to manipulate the HTTP response message. Here is the improved implementation:
    
    public HttpResponseMessage PostProduct(Product item)
    {
        item = repository.Add(item);
        var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);
    
        string uri = Url.Link("DefaultApi", new { id = item.Id });
        response.Headers.Location = new Uri(uri);
        return response;
    }

    I don't want to provide the data within the web service which is what the products tutorial is doing. 

    Yes, separation of concern by removing DAL (Data access layer)  from the service. Move DAL into another class library project, add reference in Web API project.

    I have been trying to get this work work by changing the url to have http://localhost:xxxx/api/MyValues but won't work.  Even trying the default...it still renders the products page after I removed all of the products files and cleared the cache.

    Post current implementation for further suggestion.


    Web API sample can be downloaded at http://www.asp.net/web-api/samples , first one is the Product solution.

    Friday, March 1, 2013 6:13 AM
  • User1513064915 posted

    I haven't been able to find anything explaining the process of TESTING POST method as if I had a separate application running the service URI.   Since how else i would test the web service the same way me giving it to a user.   Sorry that I'm not getting that part.  Seems very straightforward but I was assuming it was a regular web form post of files but it appears its not.

    Friday, March 1, 2013 2:48 PM
  • User220959680 posted

    find anything explaining the process of TESTING POST method as if I had a separate application running the service URI.  

    Filler is open source tool to test the same. 

    Please refer http://www.codersphere.com/sean-mcalinden/blog/Testing-ASPNet-Web-API-using-fiddlers-composer to test your service using Fiddler. 

    Note that this tutorial walks through testing the Web API (service) on client side using HttpClient and other classes. 

    Monday, March 4, 2013 6:11 AM
  • User1513064915 posted

    Ok.  I'm almost home.  I have the GET working but the POST is not working.  So I'm assuming I have to call the POST method somewhere.  I followed the tutorial with the POST but its not firing.  Your assistance has been very helpful.

    Monday, March 4, 2013 6:38 PM
  • User220959680 posted

    but the POST is not working. 

    what is the exception?

    Please copy the source & elaborate the steps taken for further help.

    Tuesday, March 5, 2013 4:41 AM
  • User1513064915 posted

    I'm not getting any exceptions.  But when I run it....only the get is working.  The Post doesn't fire.  Not sure if I have a setting wrong or what.

    Tuesday, March 5, 2013 9:57 AM
  • User220959680 posted

    I'm not getting any exceptions.  But when I run it....only the get is working.  The Post doesn't fire.  Not sure if I have a setting wrong or what.

    Go through this video tutorial first http://www.asp.net/web-api/videos/getting-started/delete-and-update 

    * Ignore the video title. It walks through POST to create a new record. 

    Then investigate your source or the testing process.

    when it is still the same, please explain how you are testing in detail. Also with your implementation (source code).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 5, 2013 10:24 AM