How can I remove backslash when log format use escape=json

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I remove backslash when log format use escape=json

wld75
Hi, I want to log my entire request_body, but access.log contains some
strange backslash, how can I remove these backslash before doube quote?

Here is my nginx.conf,
```
log_format  main escape=json '$request_body';

 access_log  logs/access.log  main;
```

This is my request code:
```
fetch('http://localhost:8080/njs',{
method:'POST',
body:JSON.stringify({
text:'message with backslash'
})
}).then(res=>res.json()).then((res)=>{
console.info(res)
})
```

And access.log
```
{\"text\":\"message with backslash\"}
```

But I think it should be
```
{"text":"message with backslash"}
```

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,281634,281634#msg-281634

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: How can I remove backslash when log format use escape=json

Maxim Dounin
Hello!

On Thu, Oct 18, 2018 at 09:29:31AM -0400, kimown wrote:

> Hi, I want to log my entire request_body, but access.log contains some
> strange backslash, how can I remove these backslash before doube quote?
>
> Here is my nginx.conf,
> ```
> log_format  main escape=json '$request_body';
>
>  access_log  logs/access.log  main;
> ```
>
> This is my request code:
> ```
> fetch('http://localhost:8080/njs',{
> method:'POST',
> body:JSON.stringify({
> text:'message with backslash'
> })
> }).then(res=>res.json()).then((res)=>{
> console.info(res)
> })
> ```
>
> And access.log
> ```
> {\"text\":\"message with backslash\"}
> ```
>
> But I think it should be
> ```
> {"text":"message with backslash"}
> ```

With "escape=json", nginx will escape variables to be usable in
JSON structures, so you will be able to use them in log format
with JSON formatting, e.g.:

log_format main escape=json '{ "body": "$request_body" }';

If you want nginx to do not escape anything in your logs, you can
use "escape=none".  Note though that without escaping you
completely depend on data the variable contain - in particular, in
your case a malicious client will be able to supply arbitrary
data, including multiple log entries or broken records.

--
Maxim Dounin
http://mdounin.ru/
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: How can I remove backslash when log format use escape=json

wld75
I see, really thanks to your advice.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,281635,281642#msg-281642

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx