Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with GetMethod with Body request - RestSharp (DotnetCore 2.2) #1405

Closed
ramgkn opened this issue Jan 14, 2020 · 4 comments
Closed

Issue with GetMethod with Body request - RestSharp (DotnetCore 2.2) #1405

ramgkn opened this issue Jan 14, 2020 · 4 comments

Comments

@ramgkn
Copy link

ramgkn commented Jan 14, 2020

This is a continuation of issue #1399

I followed the suggestions recommended by you with "proper typed object as a parameter" with the same issue that

{"errors":{"":["A non-empty request body is required."]},"title":"One or more validation errors occurred.","status":400,"traceId":"0HLSKFCCSHR42:00000001"}

Interestingly when i updated RestSharp to 16.10.1, i am getting a different message the GET method does not support body.

var request1 = new RestRequest(Method.GET);
request1.AddHeader("Content-Type", "application/json");
request1.AddHeader("Authorization", "Bearer 111");

var test = new SearchRequest()
{
ABC= "1253",
EFG = "5254",
Name = new SearchRequestName()
{
Title = "",
Forename = "",
Mid_Initial = "",
Surname = "",
Suffix = ""
},
Dob = "",
Address = new SearchRequestAddress()
{
Sub_Bld_Name = "",
House_Name = "",
House_Nb = "",
Street = "",
District = "",
Town = "",
County = "",
Postcode = ""
}
};

 request1.RequestFormat = DataFormat.Json;

    var SerializedJsonBody = JsonConvert.SerializeObject(requestBody);

    request1.AddJsonBody(test );

response = apiService.Execute(request);

Could you please help us here.

To my understanding Resharp support "httpclient", please correct me if i am wrong.

Your assistance would be appreciated.

@alexeyzimarev
Copy link
Member

There are quite a few issues with your code, I repeatedly see the same mistake being made by developers using RestSharp:

  • Do not add content type manually, AddJsonBody does it for you.
  • Do not set the RequestType manually, again, AddJsonBody does it for you
  • Do not use serialized content as AddJsonBody parameter, you should give it your object and it will serialize it for you. Now, you are telling the serializer to serialize the string, which makes no sense.

All of it is described in the documentation https://restsharp.dev/usage/parameters.html#addjsonbody

Concerning GET requests with body - it never actually worked. Previously we were ignoring body parameters for GET requests, now we started to throw. We had several issues discussing it, like #1330. The reason for RestSharp not to support body for GET requests is that HttpWebRequest doesn't support it.

Currently, RestSharp uses HttpWebRequest. It is quite old and considered legacy, so I started working on implementing requests based on HttpClient, but I need time to finish the implementation. It is tracted on #1406.

@alexeyzimarev alexeyzimarev changed the title Issue with GetMethod with Body request - RestSharp (DotnetCore 2.2) 14-Jan-2019 Issue with GetMethod with Body request - RestSharp (DotnetCore 2.2) Jan 14, 2020
@ramgkn
Copy link
Author

ramgkn commented Jan 15, 2020

Thank you @alexeyzimarev for the comments; awaiting for the version with HttpClient implemented.

@alexeyzimarev
Copy link
Member

I am going to close it, it should be auto-resolved by the HttpClient implementation. Until then, we will throw an exception when people try using GET with a body.

@biapar
Copy link

biapar commented Apr 20, 2020

When planned?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants