# How to pass data via withHeader('Location', ...)?

**URL:** <https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429>\
**Category:** Questions\
**Created:** [June 22, 2016, 4:56pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429 "2016-06-22T16:56:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bladefinor](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@bladefinor](https://discourse.slimframework.com/u/bladefinor)\
**Post date:** [June 22, 2016, 4:56pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429/1 "2016-06-22T16:56:41Z")

</div>

My scenario is like this:

1. One goes to /test/create.
2. If GET, redirect to /test and pass an array with information: `Array('foo' => 'bar')`.
3. /test should now be able to read this array somehow.

I’ve tried using this:

```
$app->get('/test', function($request, $response, $args) {
  $body = $request->getParsedBody();
  $foo = $body['foo'];

  echo $foo;
});
    
$app->get('/test/create', function($request, $response, $args) {
  return $response->withStatus(302)->withJson(array('foo' => 'bar'))->withHeader('Location', '/test');
});

```

But nothing seems to get passed to the $body. I am new to Slim, so I believe I am doing this completely wrong.

Thanks in advance,  
bladefinor

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [June 22, 2016, 5:04pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429/2 "2016-06-22T17:04:59Z")

</div>

I think it should be this…

```auto
return $response->withJson(array('foo' => 'bar'))->withRedirect('/test');

```

---

<div class="post-metadata">

**Author:** ![bladefinor](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@bladefinor](https://discourse.slimframework.com/u/bladefinor)\
**Post date:** [June 22, 2016, 5:14pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429/3 "2016-06-22T17:14:15Z")

</div>

Although a much cleaner line, it made no noticeable difference. How do I retrieve the array?

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [June 22, 2016, 5:23pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429/4 "2016-06-22T17:23:50Z")

</div>

It could be a PHP “feature”… To confirm you can var\_dump the response… but I do not see why the body wouldn’t be dumped.

---

<div class="post-metadata">

**Author:** ![bladefinor](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@bladefinor](https://discourse.slimframework.com/u/bladefinor)\
**Post date:** [June 22, 2016, 5:29pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429/5 "2016-06-22T17:29:33Z")

</div>

It does print `$response` if I only do `print_r($response)`:

```
Slim\Http\Response Object
(
    [status:protected] => 200
    [reasonPhrase:protected] => 
    [protocolVersion:protected] => 1.1
    [headers:protected] => Slim\Http\Headers Object
        (
            [data:protected] => Array
                (
                    [content-type] => Array
                        (
                            [value] => Array
                                (
                                    [0] => text/html; charset=UTF-8
                                )
                            [originalKey] => Content-Type
                        )
                )
        )
    [body:protected] => Slim\Http\Body Object
        (
            [stream:protected] => Resource id #63
            [meta:protected] => 
            [readable:protected] => 
            [writable:protected] => 
            [seekable:protected] => 
            [size:protected] => 
        )
)

```

As you can see, no passed array. I am redirecting from /test/create using this:

`return $response->withJson(array('foo' => 'bar'))->withRedirect('/test', 302);`

Shouldn’t the $response status be 302 then? (still says 200 in the print above)

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [June 22, 2016, 5:43pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429/6 "2016-06-22T17:43:15Z")

</div>

dump the body of the message. it will be in there

---

<div class="post-metadata">

**Author:** ![bladefinor](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@bladefinor](https://discourse.slimframework.com/u/bladefinor)\
**Post date:** [June 22, 2016, 5:46pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429/7 "2016-06-22T17:46:51Z")

</div>

Dump what message? Can you give me an example code? Sorry, I am new to Slim.

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [June 22, 2016, 5:48pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429/8 "2016-06-22T17:48:15Z")

</div>

```auto
$response = $response->withJson(array('foo' => 'bar'))->withRedirect('/test');
var_dump((string)$response->getBody());

```

---

<div class="post-metadata">

**Author:** ![bladefinor](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@bladefinor](https://discourse.slimframework.com/u/bladefinor)\
**Post date:** [June 22, 2016, 5:54pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429/9 "2016-06-22T17:54:24Z")

</div>

That won’t redirect me to /test. It will only print the array from /test/create.

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [June 22, 2016, 6:14pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429/10 "2016-06-22T18:14:25Z")

</div>

Sorry,

Ok. This is why it is not going to work.

1. You return a 302 with a Body.
2. The browser goes, oh … ok. Then makes a new GET request to the place defined by your Location header.  
IT DOES NOT COPY THE RESPONSE BODY TO THE NEW REQUEST.

If you want to transfer data, you will need to use a session or a cookie.

---

<div class="post-metadata">

**Author:** ![bladefinor](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@bladefinor](https://discourse.slimframework.com/u/bladefinor)\
**Post date:** [June 22, 2016, 6:18pm UTC](https://discourse.slimframework.com/t/how-to-pass-data-via-withheader-location/429/11 "2016-06-22T18:18:16Z")

</div>

Thanks for the explanation. I solved it now with Flash messages instead.
