a week ago
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!
a week ago
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?