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

.AddJson Body does not properly handle already serialized strings with escape characters #1199

Closed
Bumler opened this issue Oct 16, 2018 · 5 comments

Comments

@Bumler
Copy link

@Bumler Bumler commented Oct 16, 2018

Expected Behavior

Given a serialized string in the .AddJsonBody() with escape characters the string should be sent as is.

Actual Behavior

In the .AddJsonBody() method when sending over a string like
"data": [ \n
it is converted to
\"data\":[\\n
in the value of the parameter. This means that in the actual network request I'm trying it's not sending json data but a string with escapes all over it.

Steps to Reproduce the Problem

  1. Use .AddJsonBody on a request and give a serialized string with escape characters as the object.
    (Optional) use a network tracking tool to see the request that is sent

Workaround

.AddParameter( "application/json", jsonString, ParameterType.RequestBody );
Achieves the same idea but works with a string.

Specifications

  • Version: 106.5.4 (this worked in 106.5.2)
@alexeyzimarev

This comment has been minimized.

Copy link
Member

@alexeyzimarev alexeyzimarev commented Oct 16, 2018

AddJsonBody is meant to add an object that we serialise to json. It works as designed.

@alexeyzimarev

This comment has been minimized.

Copy link
Member

@alexeyzimarev alexeyzimarev commented Oct 16, 2018

But if it worked I will look at it. However, there were no changes in this method between 106.5.2 and 106.5.4, I doubt the observation.

@zzanol

This comment has been minimized.

Copy link
Contributor

@zzanol zzanol commented Oct 16, 2018

This is related to #1189. Reverting #1185 was the right choice.

Rather than trying to fix SimpleJson, perhaps JsonSerializer should be detecting if the object is already json before handing off the object to SimpleJson for serialization.

        public string Serialize(object obj)
        {
            if (obj is string value)
            {
                string trimmed = value.Trim();
                if (trimmed.StartsWith("{") && trimmed.EndsWith("}"))
                {
                    return value;
                }

            }

            return SimpleJson.SerializeObject(obj);
        }
@alexeyzimarev

This comment has been minimized.

Copy link
Member

@alexeyzimarev alexeyzimarev commented Oct 20, 2018

I consider this as fixed after the merge, thanks @zzanol. I agree that fixing SimpleJson was not a very good move. In 107 I plan to get rid of SimpleJson and get back to Newtonsoft.Json.

@vk5880

This comment has been minimized.

Copy link

@vk5880 vk5880 commented Dec 6, 2018

AddBody method is kind of misleading, because it serializes the object passed. What was expected is to pass body string in raw format. Please add a method to set body in raw format with string/byte[] parameter. This will avoid confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.