NightOwl888 opened a new issue, #1176:
URL: https://github.com/apache/lucenenet/issues/1176

   ### Is there an existing issue for this?
   
   - [x] I have searched the existing issues
   
   ### Task description
   
   The `HttpClientBase` implementation is currently:
   
   
https://github.com/apache/lucenenet/blob/53f020be99b7fa5196666a471a38fe9f9a8dbdce/src/Lucene.Net.Replicator/Http/HttpClientBase.cs#L173-L188
   
   There is only one serialization option, and it is JSON. This does not match 
the upstream code:
   
   
https://github.com/apache/lucene/blob/releases/lucene-solr/4.8.1/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java#L156-L167
   
   ```java
     /**
      * <b>internal:</b> execute a request and return its result
      * The <code>params</code> argument is treated as: 
name1,value1,name2,value2,...
      */
     protected HttpResponse executePOST(String request, HttpEntity entity, 
String... params) throws IOException {
       ensureOpen();
       HttpPost m = new HttpPost(queryString(request, params));
       m.setEntity(entity);
       HttpResponse response = httpc.execute(m);
       verifyStatus(response);
       return response;
     }
   ```
   
   Per ChatGPT, a better translation of this method is:
   
   ```c#
       protected virtual HttpResponseMessage ExecutePost(string request, 
HttpContent content, params string[] parameters)
       {
           EnsureOpen();
           var req = new HttpRequestMessage(HttpMethod.Post, 
QueryString(request, parameters))
           {
               Content = content
           };
           var resp = client.Send(req);
           VerifyStatus(resp);
           return resp;
       }
   ```
   
   Which can then be used like:
   
   ```c#
       [Test]
       public void TestExecutePostRaw()
       {
           var client = new TestClient();
           var raw = "hello world";
   
           var content = new StringContent(raw);
           var resp = client.ExecutePost("http://localhost:5000/echo";, content);
           var body = resp.Content.ReadAsStringAsync().Result;
   
           Assert.AreEqual(raw, body);
       }
   ```
   
   Or, for JSON using System.Text.Json:
   
   ```c#
   // JSON
   var jsonContent = new StringContent(JsonSerializer.Serialize(entity), 
Encoding.UTF8, "application/json");
   ExecutePost("/myendpoint", jsonContent);
   ```
   
   This allows the client to decide what type of content goes into the body 
instead of always assuming it will be JSON. But it still supports JSON. It also 
means that we can drop the dependency on Newtonsoft.Json, since this is the 
only place in our solution that depends on it directly.
   
   Unfortunately, there are no tests covering this method (no callers at all), 
so we should create some. Ideally, they would be backported to Java to verify 
the port. I asked ChatGPT to frame some up:
   
   https://chatgpt.com/share/68bc2717-e4f4-8005-8a53-f4e4bf7417e0
   
   But, I don't agree with ChatGPT that we should keep a JSON overload since it 
requires NewtonSoft.Json or another JSON serializer (this overload could be 
provided as an extension method in `lucenenet-extensions`, though).
   
   Note it would also be good to check whether this fixes the reported bad use 
of HTTP as specified in the comment, but this may fix those issues.
   
   ```c#
   //.NET Note: No headers? No ContentType?... Bad use of Http? 
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to