VApteesriaustralia-com-au-esridist

createReplica API with JSON object as request parameters

Discussion created by VApteesriaustralia-com-au-esridist Employee on Nov 14, 2013
Latest reply on Oct 17, 2017 by 25394
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);
                    stream.Flush();
                    stream.Close();
                }
                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"


Cheers,
Vish

Outcomes