0

I got a REST based application that does POST request. I have a new endpoint that needs to upload an HTLM file with escaped quotes (i.e. precede " characters with ).

Python used quotes "" for strings in the code, so this is a little tricky and I tried to many variations trying to keep my HTML content with escaped quotes, unsuccessfully. So here is the source HTML, just an extract (I used triple quotes to be able to use quotes in the string):

htmlScript = """?xml version=\"1.0\" encoding=\"UTF-8\"?"""

print htmlScript?xml version="1.0" encoding="UTF-8"?

The HTML content is then passed into a dictionary variable called postData, where the HTML still looks fine:

postData = {'transactionScript': '?xml version="1.0" encoding="UTF-8"?', 'alertsEnabled': 0, 'interval': 300, 'testName': 'Valerio API_2015-08-11 13:05:02.644245', 'timeLimit': 30, 'agents': [{'agentId': 4107}, {'agentId': 58}]}

Then I need to use the json.dumps as I do with the other endpoints of my application:

data=json.dumps(postData)

Here is the problem, the results is with quotes with double backslash in the front:

print data
'{"transactionScript": "?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?", "alertsEnabled": 0, "interval": 300, "testName": "Valerio API_2015-08-11 13:05:02.644245", "timeLimit": 30, "agents": [{"agentId": 4107}, {"agentId": 58}]}'

QUESTION: how can I get the HTML with quotes single escaped after the json.dumps?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Wally
  • 1
  • 1

1 Answers1

0

I presume in your last example you entered data or print repr(data) in the console? This prints a representation of the string where a backslash character is encoded as \\ to distinguish from the single \ operator used for escaping. See this question for more details.

If you print the string itself with print data you should see that quotes are escaped with only on backslash as you expect.

Community
  • 1
  • 1
Julian Go
  • 4,442
  • 3
  • 23
  • 28