Home > Blockchain >  Quote in POST content causing bad requests
Quote in POST content causing bad requests

Time:10-07

I'm doing POST requests to an API via a Windows Service written in C# and the content varies. Some of the time, the users will use " to mean inches. This data must be passed to the API, but when it's done automagically, the way I've set it up, this causes it to end the description string in the POST content prematurely, resulting in a bad request.

For example, I might have a "description" field that looks like this:

"Replaced 4" pipe"

Is there a way to pass the " in the middle of the string without exiting the string? Could it be something like the following, or should I just replace the " character with the word "inch"? I would much prefer to send the actual double quote, since I'm not certain that they would always mean it to mean inches. i.e. they may actually be quoting someone.

        if (description.Contains("\""))
        {
            description.Replace("\"", "inch");
        }

CodePudding user response:

I think I have figured out what I'm after while messing around in a console app. I'm going to try this and see if it gets me where I want to go:

if (description.Contains("\""))
{
    description.Replace("\"", @"\""");
}

This should replace a single instance of a double quote (") with an escaped double quote (\"). So the part of the string that puts in the description would look something like this:

\"description\":\""   "Replaced 4\" pipe"
  • Related