Surreal-Net/Surreal.Net

Bug: REST client implementation

Closed this issue · 6 comments

The current REST client implementation does not pass tests.
This the cause needs to be analyzed and subsequently fixed.

I have hit an interesting roadblock in debugging the rest client, namely that the following request returns a 405:

using System.Text;

HttpClient client = new();
client.BaseAddress = new("http://127.0.0.1:8082/");
client.DefaultRequestHeaders.Add("DB", "test");
client.DefaultRequestHeaders.Add("NS", "test");
client.DefaultRequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("root:root")));
// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var rsp = await client.PostAsync("key/person", new StringContent("{\"title\":\"Founder & CEO\",\"name\":{\"first\":\"Tobie\",\"last\":\"Morgan Hitchcock\"},\"marketing\":true,\"identifier\":1465455984}", Encoding.UTF8, "application/json"));

var u = rsp.RequestMessage.RequestUri;
var h = string.Join("\n", rsp.RequestMessage.Headers.Select(h => h.Key + ":" + string.Join(", ", h.Value)));
var b = await rsp.RequestMessage.Content.ReadAsStringAsync();
var r = await rsp.Content.ReadAsStringAsync();
Console.WriteLine("Hello, World!");

Whereas a REST client (Insomnia in my case) executes the exact same request successfully.

I'll have a looksie whether the client needs to be configured in any way to work

Method:
POST

Url:

http://127.0.0.1:8082/key/person/

Headers:

DB:test
NS:test
Authorization:Basic cm9vdDpyb290
Accept:application/json

Body:

{
  "title": "Founder & CEO",
  "name": { "first": "Tobie", "last": "Morgan Hitchcock" },
  "marketing": true,
  "identifier": 1465455984
}

Response:

{
  "code": 405,
  "details": "Request content length too large",
  "description": "The requested http method is not allowed for this resource. Refer to the documentation for allowed methods."
}

Method: POST

Url:

http://127.0.0.1:8082/key/person/

Headers:

DB:test
NS:test
Authorization:Basic cm9vdDpyb290
Accept:application/json

Body:

{
  "title": "Founder & CEO",
  "name": { "first": "Tobie", "last": "Morgan Hitchcock" },
  "marketing": true,
  "identifier": 1465455984
}

Response:

{
  "code": 405,
  "details": "Request content length too large",
  "description": "The requested http method is not allowed for this resource. Refer to the documentation for allowed methods."
}

adding Content-Type: application/json as a header should resolve this

Du-z commented

It is almost fixed, see #15

@Du-z Awesome! I did a few fixes, now we got a working rest client! Thanks again for your help!

Closing the issue now