I have to send a string request to a backend. I have 2 variables which I have to format inside the string with the request. The request has to look like this:
"{\"MachineType\":0,\"BrokenMachines\":6}"
I have 2 variables called type
and count
which I want to put inside the request (the 0 in the example would be type
and the 6 would be count
). I've tried almost everything I know and I'm still not able to get the string with the escaped characters, python converts them to literals. These are some solutions I tried with their results:
Attempt:
("{\"MachineType\":" + str(type) + ",\"BrokenMachines\":" + str(count) + "}").encode('unicode_escape').decode('ASCII')
Result:
'{"MachineType":1,"BrokenMachines":6}'
Attempt:
'{{\"MachineType\":{},\"BrokenMachines\":{}}}'.format(type, count).encode('unicode_escape').decode('ASCII')
Result:
'{"MachineType":1,"BrokenMachines":6}'
Attempt:
"{{\\\"MachineType\\\":{},\\\"BrokenMachines\\\":{}}}".format(type, count)
Result:
'{\\"MachineType\\":1,\\"BrokenMachines\\":6}'
How can I make it work?