How can I add \n(newline) In Json string

Can you please share that documentations link here.

also might help

Thanks I will look into in.

Did you try it with double \n
e.g.,
data = '{JsonString}\\n'

This sequence produces two characters at the end of the string \ and n. Not something expected in the NDJSON format. For NDJSON there should be the LF character: '\n' or the CR LF sequence should also be accepted: '\r\n'.

Unfortunately we do not know what exactly has @Harsh tried.

I suspect that the problem could also be in the unescaped double quotes in the JSON string values as I wrote earlier. In the Python string it should be '\\"' instead of wrong '\"'. Or problem could be the spaces shown above if they are really present in the HTTP payload.

The comparison of the network communication captures can resolve the problem easily if done properly.


The output of tcpdump with the option -X looks like below. Hexadecimal values of the captured bytes are important when we cannot distinguish them by their ASCII representation (like the dot).

$ sudo tcpdump -nXc1 host 8.8.8.8 2>/dev/null & sleep 0.1 ; ping -c1 -p"$(xxd -pu <<< 'Hello world!')" 8.8.8.8 > /dev/null
10:12:32.145469 IP 172.17.105.84 > 8.8.8.8: ICMP echo request, id 50241, seq 1, length 64
        0x0000:  4500 0054 18fc 4000 4001 fc37 ac11 6954  E..T..@.@..7..iT
        0x0010:  0808 0808 0800 cc8e c441 0001 f05f 4e63  .........A..._Nc
        0x0020:  0000 0000 3138 0200 0000 0000 6c6f 2077  ....18......lo.w
        0x0030:  6f72 6c64 210a 4865 6c6c 6f20 776f 726c  orld!.Hello.worl
        0x0040:  6421 0a48 656c 6c6f 2077 6f72 6c64 210a  d!.Hello.world!.
        0x0050:  0000 0000                                ....

Yes I have tried that also but it did not solve my problem.

Yes I received data in that format but I edited it for clear format. I will share you the same output of tcpdump using -X option.

Please before trying to capture the communication again, fix the two things I mentioned above and show us the current string you are sending in the HTTP request - exactly how it is in your Python code (no whitespace inserted). Of course you can replace any sensitive alphanumeric characters. Replace them by something unique like * (but x should be OK too) so we can better see what was replaced.

I will not compare every single character difference for you (it is up to you). I can point you to obvious problems and differences.

The tcpdump’s -X output is not suitable for making a diff easily. It would be much better if you extract the binary payload of the TCP stream using for example Wireshark or Scapy:

No: It absolutely is Python. He is getting literally ‘\n’ in the string rather than a newline. This is tricky to handle properly in python.

@Harsh have u tried setting content-type header value as application/x-ndjson
This solved the problem for me

Same problem here, using the content type of application/x-ndjson didn’t change anything.

In my case, what I need to send in the headers for my authorization token is this (changed the actual text, but kept the same general format):

headers = {
  'Authorization': 'Bearer lksajdjfa876sdfghasdfkgh.
lkjsadoiubvtoiuzxcvbl876bjhkg42387bzxljkb43087'
}

but python is instead sending this which results in an invalid character error (from the server) from the \n:

headers = {
  'Authorization': 'Bearer lksajdjfa876sdfghasdfkgh.\nlkjsadoiubvtoiuzxcvbl876bjhkg42387bzxljkb43087'
}

It works in curl, but I’d really prefer not to have to use an os call to make the request.

Can you show the successful curl command? That looks like an invalid header, but maype curl is just stripping out the newline altogether. Try it without the \n and see if it works.