Community
Showing results for 
Search instead for 
Do you mean 
Reply

_very_ strange thing with PATCH request

Copper Contributor
Posts: 189
Country: Netherlands

_very_ strange thing with PATCH request

Can you guys please have a look at this?

 

Let me begin with this: when using POSTMAN to do a Patch request, everything works fine:

 

URL:

https://<server>/act.web.api/api/contacts/20f986fb-3b2b-4508-b6c2-ac7e50485a1c

HEADERS:

Content-Type: application/json

Authorization: Bearer <token>

BODY:

{"id":"20f986fb-3b2b-4508-b6c2-ac7e50485a1c","customFields":{"lat":"100","lng":"200"}}

 

I get a JSON response with the updated contact, as expected.

 

When using a C# WebClient , with this code:

 

try
            {
                wc = new WebClient();
                wc.Encoding = Encoding.UTF8;
                wc.Headers.Add("Authorization", "Bearer " + bearerToken);
                wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                System.Net.ServicePointManager.Expect100Continue = false;                
                Uri uri = new Uri(customer.ActWebAPI_URL + "/api/contacts/" + Id);
                string upload = JsonConvert.SerializeObject(patchFields, Formatting.None);
                string JSON = wc.UploadString(uri, "PATCH", upload);
            }
            catch (Exception error)
            {
                if (error is WebException)
                {
                    var responseStream = ((WebException)error).Response?.GetResponseStream();

                    if (responseStream != null)
                    {
                        using (var reader = new StreamReader(responseStream))
                        {
                            string responseText = reader.ReadToEnd(); // the strange HTML response...
                            return new Exception(error.ToString(), new Exception(responseText));
                        }
                    }
                }

                return error;
            }

 

I get this HTML response:

 

"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>417 Expectation Failed</title>\n</head><body>\n<h1>Expectation Failed</h1>\n<p>The expectation given in the Expect request-header\nfield could not be met by this server.\nThe client sent<pre>\n Expect: 100-continue\n</pre>\n</p><p>Only the 100-continue expectation is supported.</p>\n</body></html>\n"

 

I've tried to do this:

System.Net.ServicePointManager.Expect100Continue = false;

but that didn't help. Nothing seems to help.

 

 

When using a C# HttpClient, with this code:

 

try
            {
                Uri uri = new Uri(customer.ActWebAPI_URL + "/api/contacts/" + Id);
                string upload = JsonConvert.SerializeObject(patchFields, Formatting.None);

                hc = new HttpClient();
                hc.DefaultRequestHeaders.Authorization
                         = new AuthenticationHeaderValue("Bearer", bearerToken);
                HttpResponseMessage resp = hc.PatchAsync(uri, new StringContent(upload, Encoding.UTF8, "application/json-patch+json")).Result;
            }
            catch(Exception error)
            {

            }

I can also see the response headers, which are even stranger:

 

{StatusCode: 417, ReasonPhrase: 'Expectation Failed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: close
Date: Thu, 07 Feb 2019 15:57:20 GMT
Server: Apache/2.4.6
Server: (CentOS)
Server: OpenSSL/1.0.2k-fips
Content-Length: 364
Content-Type: text/html; charset=iso-8859-1
}}

 

Apache ?!? It runs on IIS! CentOS? It runs on Windows!

Greetings,

ACT Certified Consultants
TendenZ
the Netherlands
Copper Contributor
Posts: 189
Country: Netherlands

Re: _very_ strange thing with PATCH request

I actually have this working now, by overriding the .NET WebClient this way:

 

public class WebClientModifiedForAct : WebClient
    {
        protected override System.Net.WebRequest GetWebRequest(Uri address)
        {
            System.Net.WebRequest request = base.GetWebRequest(address);
            if (request is System.Net.HttpWebRequest)
            {
                var hwr = request as System.Net.HttpWebRequest;
                hwr.ServicePoint.Expect100Continue = false;
            }
            return request;
        }
    }

 

But.. why is this needed?

Greetings,

ACT Certified Consultants
TendenZ
the Netherlands
Highlighted
Employee
Posts: 113
Country: USA

Re: _very_ strange thing with PATCH request

I can't tell from the post what protocol you are using (HTTP or HTTPS).  Can't seem to reproduce your issue with the same exception, I can get a rejected response if I use https without configuring a cert. or disabling ssl/tls.

 

I would suspect that you are making a https request without a certificate defined and the API is rejecting the call.

 

You can confirm this by either changing the URL to http:// or for now just override the Certificate with this call.

 

     ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

 

 

My test is pretty much the same as yours, without the

System.Net.ServicePointManager.Expect100Continue = false; 

 

class Program
	{
		static void Main(string[] args)
		{
			var contact = new Contact()
			{
				Id = new Guid("161059df-5836-4f88-aa66-c8aa9d1f5542"),
				FirstName = "Goldie"
			};

			try
			{
				var token = "[token goes here]";

				ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
				WebClient wc = new WebClient {Encoding = Encoding.UTF8};
				wc.Headers.Add("Authorization", "Bearer " + token);
				wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
				Uri uri = new Uri("https://localhost/act.web.api/api/contacts/161059df-5836-4f88-aa66-c8aa9d1f5542");
				string upload = JsonConvert.SerializeObject(contact, Newtonsoft.Json.Formatting.None);
				string JSON = wc.UploadString(uri, "PATCH", upload);
			}
			catch (Exception error)
			{
				if (error is WebException)
				{
					var responseStream = ((WebException)error).Response?.GetResponseStream();

					if (responseStream != null)
					{
						using (var reader = new StreamReader(responseStream))
						{
							string responseText = reader.ReadToEnd(); // the strange HTML response...
							var ex = new Exception(error.ToString(), new Exception(responseText));
						}
					}
				}

				var returnException = error;
			}
		}
	}

	public class Contact
	{
		public Guid Id { get; set; }
		public string FirstName { get; set; }
	}