Single Backslash in Post Request Body

I need to send a string that contains backslash such as \n. When I send the post request, k6 escapes the backslash and it becomes \n.
How can i send a single backslash in a request body? Hardcoding the strings does not add in additional backslashes, but getting them from another source does. Checking each character individually shows single backslash in the string.

Sample code:

export function sendbackslash(payload)
{
	// payload looks like this: `\nF\x12\n\x12`
	var res = http.post(url, payload ,params)
	console.log(res)
}

response shows this as request body:
\\nF\\x12\\n\\x12

Hello @eloader,

Thanks for sharing your doubts in the k6 community, I’m here happy to try to help you.

First of all, I’d like to understand what’s your goal. So, when you say: I need to send a string that contains backslash such as \n, do you mean you want the server to receive a new line (\n), or a string containing the backslash (\) followed by an n)?

In any case, I might be wrong, but I strongly suspect that this is more related to how JavaScript manipulates strings, than something specific to k6. For instance, in this case, depending on your goal, you might need to escape the backslashes (\\n) or use String.raw().

Last but not least, I did some tests with a dummy server, and here’s what I observe:

  • When I use \\nF\\x12\\n\\x12 from k6, the server receives: \nF\x12\n\x12.
  • When I use \nF\x12\n\x12 from k6, the server receives: \nF \n , which, when printed, represent the \n a new lines.

Thanks!

1 Like