# Closing brace missing in webhook JSON

**URL:** https://meta.discourse.org/t/closing-brace-missing-in-webhook-json/50418
**Category:** Bug
**Created:** [September 20, 2016, 11:31pm UTC](https://meta.discourse.org/t/closing-brace-missing-in-webhook-json/50418 "2016-09-20T23:31:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![DanielMarquard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danielmarquard/32/74010_2.png) [@DanielMarquard](https://meta.discourse.org/u/DanielMarquard)
#### Post date: [September 20, 2016, 11:31pm UTC](https://meta.discourse.org/t/closing-brace-missing-in-webhook-json/50418/1 "2016-09-20T23:31:45Z")

</div>

I set up a webhook in Discourse today for the first time. I’ve got a great use case for it and can’t wait to finish the code. 🙂

But the post and topic event JSON sent by Discourse is not playing well with my receiving PHP script. PHP fails to parse it. Through `json_last_error_msg`, I saw that there is a syntax error somewhere in the payload, which is why `json_decode` is failing.

From the webhook’s log panel in Discourse, the request JSON validated when I tested it, so I set up a simple script to verify that PHP is receiving the same data that Discourse is reporting, but that doesn’t seem to be the case.

```
<?php

$data = file_get_contents('php://input');
print_r($data);

?>

```

Discourse receives a response that seems to show that a closing brace is omitted in the payload being received by PHP.

Here’s the well-formed JSON payload reported by Discourse:

> <https://pastebin.com/VTtgLWm9>

…and here’s what `print_r` returns to Discourse:

> <https://pastebin.com/qCcyusu9>

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [September 20, 2016, 11:35pm UTC](https://meta.discourse.org/t/closing-brace-missing-in-webhook-json/50418/2 "2016-09-20T23:35:59Z")

</div>

Any ideas @fantasticfears?

---

<div class="post-metadata">

### Author: ![fantasticfears](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fantasticfears/32/119608_2.png) [@fantasticfears](https://meta.discourse.org/u/fantasticfears)
#### Post date: [September 21, 2016, 2:36am UTC](https://meta.discourse.org/t/closing-brace-missing-in-webhook-json/50418/3 "2016-09-21T02:36:41Z")

</div>

PHP respects `Content-Length` correctly so it truncates the string in the middle. The problem is that the payload is not count in octets.

Pull request: [FIX: Content-Lenght should be the size in octets by erickguan · Pull Request #4451 · discourse/discourse · GitHub](https://github.com/discourse/discourse/pull/4451)

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [September 21, 2016, 2:43am UTC](https://meta.discourse.org/t/closing-brace-missing-in-webhook-json/50418/4 "2016-09-21T02:43:07Z")

</div>

@fantasticfears Can you check if `Excon` sets the `Content-Length` header for us?

---

<div class="post-metadata">

### Author: ![fantasticfears](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fantasticfears/32/119608_2.png) [@fantasticfears](https://meta.discourse.org/u/fantasticfears)
#### Post date: [September 21, 2016, 2:54am UTC](https://meta.discourse.org/t/closing-brace-missing-in-webhook-json/50418/5 "2016-09-21T02:54:25Z")

</div>

Yes, it does if the field is not provided. In `connection.rb`:

```ruby
body = datum[:body].is_a?(String) ? StringIO.new(datum[:body]) : datum[:body]

# The HTTP spec isn't clear on it, but specifically, GET requests don't usually send bodies;
# if they don't, sending Content-Length:0 can cause issues.
unless datum[:method].to_s.casecmp('GET') == 0 && body.nil?
  unless datum[:headers].has_key?('Content-Length')
    datum[:headers]['Content-Length'] = detect_content_length(body)
  end
end

```

```ruby
def detect_content_length(body)
  if body.respond_to?(:size)
    # IO object: File, Tempfile, StringIO, etc.
    body.size
  elsif body.respond_to?(:stat)
    # for 1.8.7 where file does not have size
    body.stat.size
  else
    0
  end
end

```

Since we need to record that field in the log, it doesn’t matter.

But it’s worthwhile to build a module for the job and redeliver action. 1) Reuse the code. 2) Easier to replace `Excon`.

---

<div class="post-metadata">

### Author: ![erlend\_sh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erlend_sh/32/119475_2.png) [@erlend\_sh](https://meta.discourse.org/u/erlend_sh)
#### Post date: [September 21, 2016, 11:48pm UTC](https://meta.discourse.org/t/closing-brace-missing-in-webhook-json/50418/6 "2016-09-21T23:48:53Z")

</div>

A post was split to a new topic: [Super impressed with Discourse’s development cycle](https://meta.discourse.org/t/super-impressed-with-discourses-development-cycle/50465)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [March 15, 2017, 2:59am UTC](https://meta.discourse.org/t/closing-brace-missing-in-webhook-json/50418/7 "2017-03-15T02:59:43Z")

</div>


