I want to send my code to this API to compile. But it throws an error cuz there is \n in my string. How can I set this string in my header?
xhr.setRequestHeader('code', 'n = 5\nwhile n > 0:\n n -= 1\n print(n)');
OR
xhr.setRequestHeader('code',
'n = 5
while n > 0:
n -= 1
print(n)'
);
This is the error I get :
Uncaught DOMException: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'n = 5
while n > 0:
n -= 1
print(n)' is not a valid HTTP header field value.
CodePudding user response:
You are not allowed to use symbol of new line in header value.
However, you can come up with a certain sequence of characters yourself, which the backend will regard as a newline character and automatically replace this sequence with a newline.
But it is better, of course, to transmit such things in the HTTP POST request body, and not in the headers.