RestSharp: Can no longer add JSON manually to a request as a parameter

Describe the bug In RestSharp v106 and earlier, it was possible to add JSON to the request using the following sequence:

request.AddParameter("application/json", myJsonString, ParameterType.RequestBody);

With RestSharp v107 this no longer works. Instead you get a request like this:

POST /soa-infra/resources/GWS/ProductAvailability/Availability HTTP/1.1
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept: application/xml, text/xml, application/json, text/json, text/x-json, text/javascript
Content-Type: application/json; boundary="030269ab-0029-48b8-b40e-d12d4445826e"
Content-Length: 197
Host: ws.specialized.com
Cache-Control: no-cache
Connection: keep-alive
--030269ab-0029-48b8-b40e-d12d4445826e
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="application/json"
System.Byte[]
--030269ab-0029-48b8-b40e-d12d4445826e--

when it should be like this:

POST /soa-infra/resources/GWS/ProductAvailability/Availability HTTP/1.1
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept: application/xml, text/xml, application/json, text/json, text/x-json, text/javascript
Content-Type: application/json
Content-Length: 232
Host: ws.specialized.com
Cache-Control: no-cache
Connection: keep-alive
{"CredentialRequest":{"Username":"XXXX", ... [rest of JSON snipped]

To Reproduce Try to add JSON manually to the body of the request as above.

Expected behavior It should behave the same as is you had called the following, which is how v107 expects you to do it:

request.AddBody(myJsonString, "application/json");

While you could argue that might be the ‘correct’ way, the problem is the old libraries fully supported adding the request body that way and the API still appears to support it as it has the correct function signature for it. Anyone porting existing code will run into this so it should be fixed.

Desktop (please complete the following information):

  • Windows 10, Windows Server 2016
  • .NET Framework
  • Version 4.8

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Adding a body parameter with name = content type

I agree that it might be a good idea to make the legacy-compatible call like the one below, to work.

request.AddParameter("application/json", myJsonString, ParameterType.RequestBody);

However, I am very unhappy about this API, because it is very implicit. The parameter name argument is the name of the parameter. It’s not for the content type. The implicit way of the old API is confusing. It should’ve never worked as it did. So, if we’d t make it work again, it needs to have a visible warning that you should not be doing it.

Using AddJsonBody

I am sorry guys but this warning is there for a reason, and I keep responding to SO questions, and I keep seeing issues opened. AddJsonBody is for objects to be serialized.

image

https://restsharp.dev/usage.html#request-body

I really would appreciate people reading the docs before claiming “it doesn’t work”.