Webhook not sending json formatted data when using Charfield with multiple lines #10988

Closed
opened 2025-12-29 21:38:50 +01:00 by adam · 5 comments
Owner

Originally created by @tux-mania on GitHub (Apr 4, 2025).

Deployment Type

Self-hosted

NetBox Version

v4.2.6

Python Version

3.11

Steps to Reproduce

  1. Create a Webhook
  2. Set HTTP content type as application/json
  3. Set HTTP Method to POST
  4. Add body template with:
    {
    "id": "{{ data["id"] }}",
    "name": "{{ data["name"] }}",
    "slug": "{{ data["slug"] }}",
    "physical_address": "{{ data["physical_address"] }}"
    }```
    
  5. Create a Event Rule to trigger Webhook on Create Site event

Expected Behavior

The data sent by the webhook should be json formatted (it is when not using multiple lines in fields).
With multiple lines on the physical_address field, it should be formatted like this:

{
  "id": 3182,
  "name": "99999-API2",
  "slug": "99999-api2",
  "physical_address": "Name of Street\r\nName of the city"
}

Observed Behavior

Instead, when providing multiple lines in physical_address field, the format gets completely messed up:

b'{
  \n"id": "3181",
  \n"name": "99999-API2",
  \n"slug": "99999-api2",
  \n"physical_address": "Name of Street\r\nName of the city"
\n}'
Originally created by @tux-mania on GitHub (Apr 4, 2025). ### Deployment Type Self-hosted ### NetBox Version v4.2.6 ### Python Version 3.11 ### Steps to Reproduce 1. Create a Webhook 2. Set `HTTP content type` as `application/json` 3. Set `HTTP Method` to `POST` 4. Add body template with: ``` { "id": "{{ data["id"] }}", "name": "{{ data["name"] }}", "slug": "{{ data["slug"] }}", "physical_address": "{{ data["physical_address"] }}" }``` 5. Create a `Event Rule` to trigger `Webhook` on `Create Site` event ### Expected Behavior The data sent by the webhook should be json formatted (it is when not using multiple lines in fields). With multiple lines on the `physical_address` field, it should be formatted like this: ``` { "id": 3182, "name": "99999-API2", "slug": "99999-api2", "physical_address": "Name of Street\r\nName of the city" } ``` ### Observed Behavior Instead, when providing multiple lines in `physical_address` field, the format gets completely messed up: ``` b'{ \n"id": "3181", \n"name": "99999-API2", \n"slug": "99999-api2", \n"physical_address": "Name of Street\r\nName of the city" \n}' ```
adam added the type: bug label 2025-12-29 21:38:50 +01:00
adam closed this issue 2025-12-29 21:38:50 +01:00
Author
Owner

@jnovinger commented on GitHub (Apr 4, 2025):

@tux-mania , for the time being, I think your best bet is to use the Jinja2 striptags filter that will condense whitespace and should eliminate the line breaks that are breaking JSON formatting. You can use it like so:

{
  "id": "{{ data["id"] }}",
  "name": "{{ data["name"] }}",
  "slug": "{{ data["slug"] }}",
  "physical_address": "{{ data["physical_address"]|striptags }}"
}

I'm unclear if your report represents expected behavior or not, as the webhook feature does allow choosing a specific content type. I will dig into it more and get back.

@jnovinger commented on GitHub (Apr 4, 2025): @tux-mania , for the time being, I think your best bet is to use the Jinja2 `striptags` filter that will condense whitespace and should eliminate the line breaks that are breaking JSON formatting. You can use it like so: ``` { "id": "{{ data["id"] }}", "name": "{{ data["name"] }}", "slug": "{{ data["slug"] }}", "physical_address": "{{ data["physical_address"]|striptags }}" } ``` I'm unclear if your report represents expected behavior or not, as the webhook feature _does allow_ choosing a specific content type. I will dig into it more and get back.
Author
Owner

@tux-mania commented on GitHub (Apr 4, 2025):

@jnovinger, thanks a lot for your answer!
Didn't know about the striptags filter. Using it, I loose the \r\n in the data, but at least the data stays correctly formatted in JSON.

I'm unclear if your report represents expected behavior or not

Sorry if I didn't explained it correctly. I know indeed we can specify the content type in the webhook. The thing I don't understand is why, when we specify application/json and use fields with multiple lines, the content type somehow breaks.

Thanks for your help.

@tux-mania commented on GitHub (Apr 4, 2025): @jnovinger, thanks a lot for your answer! Didn't know about the `striptags` filter. Using it, I loose the `\r\n` in the data, but at least the data stays correctly formatted in JSON. > I'm unclear if your report represents expected behavior or not Sorry if I didn't explained it correctly. I know indeed we can specify the content type in the webhook. The thing I don't understand is why, when we specify `application/json` and use fields with multiple lines, the content type somehow breaks. Thanks for your help.
Author
Owner

@jnovinger commented on GitHub (Apr 5, 2025):

np!

I was actually just thinking about this some more and realized there's a better way. Because I'm so used to Django templates, I often forget you can call methods on objects in Jinja2 templates, like ...

{
  "id": "{{ data["id"] }}",
  "name": "{{ data["name"] }}",
  "slug": "{{ data["slug"] }}",
  "physical_address": "{{ data["physical_address"]|.replace('\r\n', '\\r\\n') }}"
}

This escapes the newline control characters so that it is legal JSON and it maintains the newline.

@jnovinger commented on GitHub (Apr 5, 2025): np! I was actually just thinking about this some more and realized there's a better way. Because I'm so used to Django templates, I often forget you can call methods on objects in Jinja2 templates, like ... ``` { "id": "{{ data["id"] }}", "name": "{{ data["name"] }}", "slug": "{{ data["slug"] }}", "physical_address": "{{ data["physical_address"]|.replace('\r\n', '\\r\\n') }}" } ``` This escapes the newline control characters so that it is legal JSON and it maintains the newline.
Author
Owner

@tux-mania commented on GitHub (Apr 7, 2025):

Even better solution!
Thanks a lot @jnovinger for your help and your time! 🙂

@tux-mania commented on GitHub (Apr 7, 2025): Even better solution! Thanks a lot @jnovinger for your help and your time! 🙂
Author
Owner

@jnovinger commented on GitHub (Apr 7, 2025):

Talked this one over with the team. Given the wide array of possible values for content_type and the many, many different possible template constructions, we're not planning on supporting any type of opportunistic validation for rendered webhook body values.

@jnovinger commented on GitHub (Apr 7, 2025): Talked this one over with the team. Given the wide array of possible values for `content_type` and the many, many different possible template constructions, we're not planning on supporting any type of opportunistic validation for rendered webhook body values.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#10988