You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can no longer add JSON manually to a request as a parameter #1816
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):
That is not correct as body parameters have no 'name'. If you look at the implementation of how it is done in the Request.AddBody function, it needs to use the name as the content type which is how it was supported previously in v106:
Current code triage: required overloads are missing in RestSharp API
//Request initializationRestRequestuploadRequest=CreateRestRequest(URI_RESOURCE,Method.Post,"multipart/form-data");uploadRequest.AlwaysMultipartFormData=true;//doesn't work: CreateParameter always assigns ContentType = "text/plain" for ParameterType.RequestBodyuploadRequest.AddParameter(JsonParameter.CreateParameter("metadata",@"{ \"some json\"}",ParameterType.RequestBody));//doesn't work: overwrite initialized ContentType not possible (read-only property)varmetaParam=(BodyParameter)JsonParameter.CreateParameter("metadata",@"{ \"some json\"}",ParameterType.RequestBody);metaParam.ContentType="text/json";//doesn't work: AddJsonBody always adds body as JSON, but attachment name "metadata" can't be defined + not desired change by implementation code with line: request.RequestFormat = DataFormat.Json;uploadRequest.AddJsonBody<string>(@"{ \"some json\"}")//doesn't work: JsonParameter constructor doesn't provide ParameterType argument, after construction property is again read-only
uploadRequest.AddParameter(newJsonParameter("metadata" @"{ \"some json\" }","application/json"){Type=ParameterType.RequestBody});//remaining steps work again, just for informational purposes here...//add 2nd body attachment with raw file datauploadRequest.AddFile(fileName,filePath);
Proposal patch in class "Parameter": Added method CreateBodyParameter (besides method CreateParameter)
//Request initializationRestRequestuploadRequest=CreateRestRequest(URI_RESOURCE,Method.Post,"multipart/form-data");uploadRequest.AlwaysMultipartFormData=true;//WORKAROUND: create BodyParameter instance manuallyuploadRequest.AddParameter(newBodyParameter("metadata" @"{ \"some json\" }","application/json"));//remaining steps work again, just for informational purposes here...//add 2nd body attachment with raw file datauploadRequest.AddFile(fileName,filePath);
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.
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.
I really would appreciate people reading the docs before claiming "it doesn't work".
You referenced AddStringBody(this RestRequest request, string body, string contentType), but this method doesn't allow to define a name for the body. That's why it's not working for my project.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I think I have an issue connected to this one. When I need to do the same type of request as @jochenwezel. I need a multipart/form-data. Send a file and json. But the consuming API is strict and needs the body parameter Content-Type be equal to exactly "application/json".
BodyParameter a = new BodyParameter("\"itemDetail\"", createItemRequest.Serialize(), "application/json", DataFormat.None) request.AddParameter(a); request.AddFile(imageRequest.FieldId, imageRequest.Image, imageRequest.FileName, "application/octet-stream");
No, something else changes content type from Content-Type: application/json to Content-Type: application/json; charset=utf-8 . When I use multipart/form-data. I expected DisableCharset to solve it. But now I understand that is not implemented. Is there some workaround?
Activity
Fix for manual JSON body content parameter. restsharp#1816
Fix for manual JSON body content parameter. restsharp#1816
kendallb commentedon Mar 29, 2022
Ok fixed in this PR with unit test that fails without the fix, and passes with it:
#1817
The problem is this line:
RestSharp/src/RestSharp/Parameters/Parameter.cs
Line 37 in 652a4c4
That is not correct as body parameters have no 'name'. If you look at the implementation of how it is done in the Request.AddBody function, it needs to use the name as the content type which is how it was supported previously in v106:
Fix for manual JSON body content parameter. restsharp#1816
Fix for manual JSON body content parameter. restsharp#1816
Fix for manual JSON body content parameter. restsharp#1816
Fix for manual JSON body content parameter. restsharp#1816
Fix for manual JSON body content parameter. restsharp#1816
Fix for manual JSON body content parameter. restsharp#1816
Fix for manual JSON body content parameter. restsharp#1816
jochenwezel commentedon Apr 13, 2022
Might be the same issue or slightly different:
Required request data (works before v107)
Current result of Content-Type "text/plain" with v107 which doesn't work
Current code triage: required overloads are missing in RestSharp API
Proposal patch in class "Parameter": Added method CreateBodyParameter (besides method CreateParameter)
Found workaround in my solution
kendallb commentedon Apr 13, 2022
Looks like a similar issue, yes.
alexeyzimarev commentedon Apr 14, 2022
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.
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
AddJsonBodyI am sorry guys but this warning is there for a reason, and I keep responding to SO questions, and I keep seeing issues opened.
AddJsonBodyis for objects to be serialized.https://restsharp.dev/usage.html#request-body
I really would appreciate people reading the docs before claiming "it doesn't work".
jochenwezel commentedon Apr 14, 2022
You referenced
AddStringBody(this RestRequest request, string body, string contentType), but this method doesn't allow to define a name for the body. That's why it's not working for my project.1 remaining item
kendallb commentedon Apr 14, 2022
Yes the old behaviour is a nasty side effect. It's not clear if that was always intentional, or something that worked and everybody just did it.
stale commentedon Jun 23, 2022
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
repo-ranger commentedon Jun 23, 2022
jochenwezel commentedon Jun 23, 2022
@kendallb already prepared several commits - this ticket should keep open until they're accepted, isn't it?
alexeyzimarev commentedon Jun 23, 2022
Yeah, I keep reopening it. Not ready to turn off the bot yet.
stale commentedon Aug 13, 2022
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
repo-ranger commentedon Aug 13, 2022
david-siegert commentedon Feb 21, 2023
I think I have an issue connected to this one. When I need to do the same type of request as @jochenwezel. I need a multipart/form-data. Send a file and json. But the consuming API is strict and needs the body parameter Content-Type be equal to exactly "application/json".
BodyParameter a = new BodyParameter("\"itemDetail\"", createItemRequest.Serialize(), "application/json", DataFormat.None)request.AddParameter(a);request.AddFile(imageRequest.FieldId, imageRequest.Image, imageRequest.FileName, "application/octet-stream");and on the client I have set:
client.Options.DisableCharset = true,This produces:
When expected Content-Type is:
Content-Type: application/jsonalexeyzimarev commentedon Feb 21, 2023
You mean that the
charsetis breaking it? I haven't implementingDisableCharsetfor multipart forms, not even sure if it can be done.david-siegert commentedon Feb 21, 2023
No, something else changes content type from
Content-Type: application/jsontoContent-Type: application/json; charset=utf-8. When I usemultipart/form-data. I expectedDisableCharsetto solve it. But now I understand that is not implemented. Is there some workaround?alexeyzimarev commentedon Feb 22, 2023
Well, it's not something else, it's the .NET form data content class :) I need to check if it's possible to change its behaviour.