[OSM-dev] api update question

Igor Brejc igor.brejc at gmail.com
Mon Jan 21 17:25:31 GMT 2008


Rob wrote:
> i'm trying to update a node from my project (C#)
> when i do the web request, the GetResponse returns with "The remote 
> server returned an error: (417) Expectation Failed."
>
> do i need to change the content type ? or do i have to include 
> action='modify' in the node ?
>
>
>             string strXml = "<?xml version='1.0' 
> encoding='UTF-8'?><osm version='0.5' generator='OSMTAGEDITOR'><node 
> id='30226967' lat='50.8489498 ' lon='5.6878935' user='rubke' 
> timestamp='2008-01-20T14:56:52Z'><tag k='amenity' v='place_of_worship' 
> /><tag k='denomination' v='christian/catholic' /><tag k='name' 
> v='St-Servaasbasiliek' /><tag k='religion' v='christian' 
> /></node></osm>";
>
>             HttpWebRequest HttpWRequest = 
> (HttpWebRequest)WebRequest.Create("http://api.openstreetmap.org/api/0.5/node/30226967");
>             HttpWRequest.PreAuthenticate = true;
>             HttpWRequest.Credentials = new NetworkCredential("xxxx", 
> "xxxx");
>             HttpWRequest.Method = "PUT";
>             HttpWRequest.ContentLength = strXml.Length;
>             HttpWRequest.ContentType = "text/plain";
>             using (StreamWriter writer = new 
> StreamWriter(HttpWRequest.GetRequestStream()))
>             {
>                 writer.WriteLine (strXml);
>             }
>             WebResponse response = HttpWRequest.GetResponse();
>
>             using (StreamReader reader = new 
> StreamReader(response.GetResponseStream()))
>             {
>                 while ( reader.Peek() != -1)
>                 {
>                     Console.WriteLine(reader.ReadLine());
>                 }
>             }
> ------------------------------------------------------------------------
Hello Rob,

I haven't tried your code, Have you checked the HTTP traffic using some 
http tracker?
This is what MSDN says about this error:     Equivalent to HTTP status 
417. ExpectationFailed indicates that an expectation given in an Expect 
header could not be met by the server.
Also:
            HttpWRequest.ContentLength = strXml.Length;

ContentLength is set in bytes, which is not necessary the same as the 
length of your string - it depends on encoding you use. Try to 
explicitly specify encoding.

BTW: I haven't yet implemented the code for uploading to OSM server, but 
when downloading, I use WebClient class instead of 
HttpWebRequest/Response. Example (which works for me):

        public osm GetMap (double bottomLeftLng, double bottomLeftLat, 
double topRightLng, double topRightLat)
        {
            WebClient webClient = new WebClient ();

            UriBuilder requestUrl = new UriBuilder(GetCommandUrl ("map"));
            requestUrl.Query = String.Format 
(System.Globalization.CultureInfo.InvariantCulture,
                "bbox={0},{1},{2},{3}", bottomLeftLng, bottomLeftLat, 
topRightLng, topRightLat);

            byte[] data = webClient.DownloadData (requestUrl.Uri);

            osm map = null;

            using (MemoryStream stream = new MemoryStream (data))
            {
                XmlSerializer serializer = new XmlSerializer (typeof (osm));
                map = (osm)serializer.Deserialize (stream);
            }

            return map;
        }

Regards,
Igor





More information about the dev mailing list