locked
Unable to create a new record using HTTP POST using OData RRS feed

  • Question

  • Hi,

    I have created a console client application that is trying to create a new record using HTTP POST to a data service.

    I first implemented the client by importing the service reference. Creating the record this way works perfectly.

    When I try to do the same by posting the XML document to the server I receive a "(400) Bad Request" error being returned. I have tried examining the data being received by the service however, it appears to be getting data (content length > 0) but I am not able to see the actual content (i.e. the xml document received by the service in HttpContext.Current.Request.InputStream is empty).

    I have attached the client application code below:

     

    namespace ConsoleTestApp
    {
     class Program
     {
     static void CreateFuel()
     {
     XNamespace ds = "http://schemas.microsoft.com/ado/2007/08/dataservices";
     XNamespace dsmd = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
     var content =
     new XElement( dsmd + "properties",
     new XElement( ds + "Description", string.Format( "Test fuel: {0}", DateTime.Now) )
     );
    
     // Create the AtomPub entry. This is generic to AtomPub.
     XNamespace atom = "http://www.w3.org/2005/Atom";
     var entry =
     new XElement( atom + "Entry",
     new XElement( atom + "title", "",
     new XAttribute( "type", "text" ) ),
     new XElement( atom + "updated", DateTime.Now ),
     new XElement( atom + "author",
     new XElement( atom + "name", "" ) ),
     new XElement( atom + "content",
     new XAttribute( "type", "application/xml" ),
     content )
     );
    
     // Send the HTTP request
     HttpWebRequest request = ( HttpWebRequest )HttpWebRequest.Create( string.Format( "http://ncho-tch037-pt:83/NetstarDataService.svc/Fuels" ) );
    
     request.Method = "POST";
     request.KeepAlive = true;
     request.ContentType = "application/atom+xml";
     request.Accept = "application/atom+xml,application/xml";
     
     request.Headers[ "Accept-Charset" ] = "UTF-8";
     request.UserAgent = "Microsoft ADO.NET Data Services";
     request.Headers[ "DataServiceVersion" ] = "1.0;NetFx";
     request.Headers[ "MaxDataServiceVersion" ] = "3.0;NetFx";
    
     var writer = XmlWriter.Create( request.GetRequestStream() );
     entry.WriteTo( writer );
     writer.Flush();
     writer.Close();
    
     WebResponse response = request.GetResponse();
     if( ( (HttpWebResponse)response ).StatusCode == HttpStatusCode.Created /* 201 */ )
     {
     Console.WriteLine(
     "Created fuel @ Location= {0}", response.Headers[ "Location" ] );
     }
     else
     {
     Console.WriteLine( "Fuel creation failed" );
     }
     }
    
     
     static void CreateFuelWithLib()
     {
     NOMSSvc.NOMSEntities svc = new NOMSSvc.NOMSEntities( new Uri( "http://ncho-tch037-pt:83/NetstarDataService.svc" ) );
     NOMSSvc.Fuel f = new Fuel();
     f.Description = "Diesel";
     svc.AddToFuels( f );
     svc.SaveChanges();
     Console.WriteLine("Created fuel");
     }
    
     static void Main( string[] args )
     {
     Console.WriteLine( "Starting..." );
     try
     {
     CreateFuelWithLib();
     CreateFuel();
     }
     catch( Exception error )
     {
     Console.WriteLine( error.Message );
     if( error.InnerException != null )
     {
     Console.WriteLine( error.InnerException.Message );
     }
     }
    
     Console.WriteLine( "Press any key to exit" );
     Console.ReadKey();
     }
     }
    }
    
    

     

     




    Friday, April 15, 2011 7:03 AM

All replies

  • Hi,

    Could you please post an HTTP trace of the request and the response from the server? (use Fiddler or something like that).

    Thanks,


    Vitek Karas [MSFT]
    Friday, April 15, 2011 10:00 AM
    Moderator