public String executeGet() { String jsonString = null; try { // SP2013 REST API endpoint this.httpWebRequest = this.getHttpWebRequestWithNTLMCredentials(this.endpoint); // HEADERS this.httpWebRequest.Method = "GET"; this.httpWebRequest.Accept = "application/json;odata=verbose"; this.httpWebRequest.Timeout = 60 * 1000; // GetResponse() timeout. if (this.debug) { WebHeaderCollection webHeaderCollectionRequest = this.httpWebRequest.Headers; string[] requestKeys = httpWebRequest.Headers.AllKeys; Console.WriteLine(); foreach (var k in requestKeys) { Console.WriteLine("\t{0,-45} => {1}", k, webHeaderCollectionRequest.Get(k)); } } // RESPONSE HttpWebResponse httpWebResponse = (HttpWebResponse)this.httpWebRequest.GetResponse(); jsonString = readResponse(httpWebResponse); Console.WriteLine("\nHttpWebRequest {0} => {1}", this.httpWebRequest.Method, this.endpoint); if (this.debug) { WebHeaderCollection webHeaderCollectionResponse = httpWebResponse.Headers; string[] responseKeys = httpWebResponse.Headers.AllKeys; Console.WriteLine("\nHttpResponse => " + this.endpoint); foreach (var k in responseKeys) { Console.WriteLine("\t{0,-45} => {1}", k, webHeaderCollectionResponse.Get(k)); } Console.WriteLine(SP2013REST.jsonPretty(jsonString)); } } catch (Exception ex) { string msg = String.Format("\nERROR: HttpWebRequest {0} => {1}\n\n{2}\n\n", this.httpWebRequest.Method, this.endpoint, ex.Message); if (Regex.IsMatch(ex.Message, "Bad Request")) { msg += "A Bad Request means your URL was malformed somehow.\n"; msg += "Common mistakes are missing & or ? for query params.\n"; msg += "Or, the SP site url base is wrong.\n"; } throw new SP2013Exception(msg); } return(jsonString); }
public String executePost(string jsonPayload, string httpMethodOverride, string etag) { String jsonString = null; try { // All POST, PUT, DELETE, MERGE need this RequestDigest value. string digestValue = this.getFormDigestValue(); // SP2013 REST API endpoint this.httpWebRequest = this.getHttpWebRequestWithNTLMCredentials(this.endpoint); // HEADERS this.httpWebRequest.Method = "POST"; this.httpWebRequest.Accept = "application/json;odata=verbose"; this.httpWebRequest.ContentType = "application/json;odata=verbose"; this.httpWebRequest.Timeout = 60 * 1000; // GetResponse() timeout. // http://www.hanselman.com/blog/HTTPPUTOrDELETENotAllowedUseXHTTPMethodOverrideForYourRESTServiceWithASPNETWebAPI.aspx if (httpMethodOverride != null && Regex.IsMatch(httpMethodOverride, "PUT|MERGE|DELETE")) { this.httpWebRequest.Headers.Add("X-HTTP-Method", httpMethodOverride); } if (jsonPayload == null) { this.httpWebRequest.ContentLength = 0; } else { this.httpWebRequest.ContentLength = jsonPayload.Length; if (jsonPayload.Length > 0) { // Encode String jsonPayload to Byte[] Byte[] jsonPostData = System.Text.Encoding.ASCII.GetBytes(jsonPayload); // Get Stream for data json payload Stream stream = this.httpWebRequest.GetRequestStream(); stream.Write(jsonPostData, 0, jsonPostData.Length); stream.Close(); } } if (etag == null) { this.httpWebRequest.Headers.Add("If-Match", "*"); // update or delete reguardless of etag. } else { this.httpWebRequest.Headers.Add("If-Match", etag); // update or delete only if same record. } this.httpWebRequest.Headers.Add("X-RequestDigest", digestValue); if (this.debug) { Console.WriteLine("DIGEST used: " + digestValue); WebHeaderCollection webHeaderCollectionRequest = this.httpWebRequest.Headers; string[] requestKeys = this.httpWebRequest.Headers.AllKeys; Console.WriteLine(); foreach (var k in requestKeys) { Console.WriteLine("\t{0,-45} => {1}", k, webHeaderCollectionRequest.Get(k)); } } // RESPONSE HttpWebResponse httpWebResponse = (HttpWebResponse)this.httpWebRequest.GetResponse(); jsonString = readResponse(httpWebResponse); Console.WriteLine("\nHttpWebRequest {0} X-Http-Method-Override: {1} => {2}", this.httpWebRequest.Method, httpMethodOverride, this.endpoint); Console.WriteLine("\nJSON payload\n=================================="); Console.WriteLine(SP2013REST.jsonPretty(jsonPayload)); Console.WriteLine(); if (this.debug) { WebHeaderCollection webHeaderCollectionResponse = httpWebResponse.Headers; string[] responseKeys = httpWebResponse.Headers.AllKeys; Console.WriteLine("\nHttpResponse => " + this.endpoint); foreach (var k in responseKeys) { Console.WriteLine("\t{0,-45} => {1}", k, webHeaderCollectionResponse.Get(k)); } Console.WriteLine(SP2013REST.jsonPretty(jsonString)); } } catch (Exception ex) { string msg = String.Format("\nERROR: HttpWebRequest {0} X-Http-Method-Override:{1} => {2}\n\n{3}", this.httpWebRequest.Method, httpMethodOverride, this.endpoint, ex.Message); throw new SP2013Exception(msg); } return(jsonString); }