createReplica API with JSON object as request parameters

11-14-2013 05:15 PM
Esri Contributor
Hi All,

I am developing a WPF application and having some trouble with createReplica REST API call.

I am using createReplica REST API call to download layers and attachments from ArcGIS Online Feature Service. I am specifying a JSON string for API parameters with HttpWebRequest.ContentType set to "application/json; charset=utf-8".

When I call HttpWebRequest.GetResponse(), I keep getting response as html string that says "Token required" despite of requesting the response as json (f=json parameter). I have tried including "token" in the HttpWebRequest URL as well as in the JSON object but no difference. Am I missing anything? Does createReplica support JSON content type? Snippet of the code below;

string replicaParams = GenerateRepliaceRequestJSON();
                //string replicaParams = GenerateReplicaRequestForm();
                string url = string.Format(@"{0}/{1}?token={2}", _currService, "createReplica", _tokenValue);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Method = "POST";
                webRequest.ContentLength = 0;
                webRequest.ContentType = "application/json; charset=utf-8";
                //webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.Accept = "application/json";
                webRequest.Referer = txtBaseURL.Text;
                byte[] postBytes = Encoding.UTF8.GetBytes(replicaParams);
                webRequest.ContentLength = postBytes.Length;

                using (Stream stream = webRequest.GetRequestStream())
                    stream.Write(postBytes, 0, postBytes.Length);
                using (HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
                    using (StreamReader sreader = new StreamReader(response.GetResponseStream()))
                        string responseString = sreader.ReadToEnd(); // response here is html that says "Token required"

1 Reply
Occasional Contributor

I have the same question. I am using ArcRest to create replica in a .NET application. I know I need to pass in a username and password because the feature service is not public. I get the same error message you get because its looking for a token.

Just looking for an example on how this would be completed. If anyone has a an example, I would love to see it. Many thanks in advance.

The Kenerson Group

0 Kudos