<p>Got in touch with P&amp;T support and got the following response back:</p>

<blockquote>
  <p>We don't currently support parsing JSON via getBodyParams(), however I'm looking into it and looks like Yii can be configured to support that, when the request's Content-Type header is set to application/json. We would need to enable that feature ourselves though.</p>
  
  <p>In the meantime you could do:</p>

<pre><code>$body = Craft::$app-&gt;request-&gt;getRawBody();
$data = craft\helpers\Json::decode($body);
</code></pre>
</blockquote>

CC BY-SA 4.0
2019-08-09T07:24:23.440Z
Thanks for following up with them. That's helpful to know and I hope they do add this in one of the next releases.
Udo
CC BY-SA 4.0
2019-08-09T20:42:15.393Z
@Udo They did mention they would be adding it to the roadmap for a future release at some point.
Joel
CC BY-SA 4.0
2019-08-08T13:01:44.033Z
2019-08-08T13:01:44.033Z
Joel
<p>As of Craft 3.3, if you send a request to Craft with a <code>Content-Type: application/json</code> header, <code>Request::getBodyParam('property')</code> will work as expected, returning the <code>property</code> value from the JSON data in the request body.</p>

CC BY-SA 4.0
2020-02-07T08:25:03.000Z
Hey Brandon, this doesn't seem to be working for me. To be clear, I'm making a POST request to the current URL (not an action URL specifically) with the Fetch API and the body JSON includes an "action" parameter. It looks like the action is ignored and the route's HTML is returned instead.
benface
CC BY-SA 4.0
2020-02-07T13:27:35.230Z
@benface Yeah, `action` params in the body are ignored if the content-type is `application/json`, because it’s likely that the `action` param wasn’t actually intended for request routing in that case (GitHub webhooks send an `action` value in JSON, for example). So you will need to either route the request based on an `action` param in the query string, or define a URL rule in `config/routes.php`, etc.
Brandon Kelly
CC BY-SA 4.0
2020-02-08T15:26:34.417Z
Thanks Brandon, that makes sense!
benface
CC BY-SA 4.0
2019-09-03T20:20:38.220Z
2019-09-03T20:38:52.210Z
2019-09-03T20:38:52.210Z
Brandon Kelly
<p>I'm trying to use <code>getBodyParams()</code> to parse a JSON posted to a module controller action from an API. The incoming headers have <code>content-type</code> set as <code>application/json; charset=utf-8</code>, and I've tested to make sure there is a valid JSON string in the body. But when I try to get the retrieve the body  using <code>getBodyParams()</code>, I only get an empty array as a response. The <a href="https://docs.craftcms.com/api/v3/craft-web-request.html#method-getbodyparams" rel="nofollow noreferrer">documentation</a> makes it sound like it's possible to parse JSON and other content types. Does anyone know the correct way to use <code>getBodyParams()</code> to parse a JSON body?</p>

<craft3><modules>
Trouble parsing JSON with getBodyParams()
# Trouble parsing JSON with getBodyParams()

(Asked by: username_0 on 2019-08-01)

I'm trying to use `getBodyParams()` to parse a JSON posted to a module controller action from an API. The incoming headers have `content-type` set as `application/json; charset=utf-8`, and I've tested to make sure there is a valid JSON string in the body. But when I try to get the retrieve the body using `getBodyParams()`, I only get an empty array as a response. The [documentation](https://docs.craftcms.com/api/v3/craft-web-request.html#method-getbodyparams) makes it sound like it's possible to parse JSON and other content types. Does anyone know the correct way to use `getBodyParams()` to parse a JSON body?

**Tags:** craft3, modules


---

## 2 answers

### Answer by username_0 on 2019-08-08. Score: 3 | Accepted answer

Got in touch with P&T support and got the following response back:

We don't currently support parsing JSON via getBodyParams(), however I'm looking into it and looks like Yii can be configured to support that, when the request's Content-Type header is set to application/json. We would need to enable that feature ourselves though.
In the meantime you could do:
$body = Craft::$app->request->getRawBody();
$data = craft\helpers\Json::decode($body);

> **Comments:**
> **username_1**: Thanks for following up with them. That's helpful to know and I hope they do add this in one of the next releases.  
> **username_0**: @username_1 They did mention they would be adding it to the roadmap for a future release at some point.  

---

### Answer by username_2 on 2019-09-03. Score: 3

As of Craft 3.3, if you send a request to Craft with a `Content-Type: application/json` header, `Request::getBodyParam('property')` will work as expected, returning the `property` value from the JSON data in the request body.

> **Comments:**
> **username_3**: Hey Brandon, this doesn't seem to be working for me. To be clear, I'm making a POST request to the current URL (not an action URL specifically) with the Fetch API and the body JSON includes an "action" parameter. It looks like the action is ignored and the route's HTML is returned instead.  
> **username_2**: @username_3 Yeah, `action` params in the body are ignored if the content-type is `application/json`, because it’s likely that the `action` param wasn’t actually intended for request routing in that case (GitHub webhooks send an `action` value in JSON, for example). So you will need to either route the request based on an `action` param in the query string, or define a URL rule in `config/routes.php`, etc.  
> **username_3**: Thanks Brandon, that makes sense!  

---

