1

I am working with Python. Currently creating an API to get information from Mender.

I want to send a post request. In my body one of the parameter got '\n' symbol. I WANT it to stay that way otherwise my request won't work. This is a new line symbol I do not want it to be escaped as a character.

Here is my code. If I print my argument before creating the 'data' object it prints '\n' as expected. BUT if I print my 'data' object all my '\n' had been transformed into '\\n' so when I dump my 'data' object my '\n' are now '\\\\n'.

    data = {
    "identity_data": {
        "mac": args.mac_address,
        "sku": args.sku
    },
    "pubkey": args.public_key,
    "tenant_token": args.tenant_token
}
print(data)

data = json.dumps(data).encode("utf-8")
api.deviceauth.create(headers=headers, data=data)

Please is there a way to avoid this default behavior of Python ? how Am I supposed to send new line symbol in a post request ? Thank you all

Kimor
  • 532
  • 4
  • 17

1 Answers1

0

try using r'......\n' the r tells python to process it in raw form rather than considering escape sequence.

  • Although it seems to be EXACTLY what I want I tried to replace `"pubkey": args.public_key,` inside my 'data' object by `"pubkey": fr'{args.public_key}'` But I still observe the same result... – Kimor Jun 10 '20 at 15:36