danb
danb

Reputation: 10379

How do I send a request body with a post in my symfony functional test?

How do you send a request body with a post? I need to post a json document in my functional test.. symfony 1.4

Upvotes: 1

Views: 585

Answers (1)

Fotis Paraskevopoulos
Fotis Paraskevopoulos

Reputation: 1011

This is what I used recently, I found it on a blog post, which I don't recall.

    $post=array(
        "a"=>"one",
        "b"=>"two",
        "c"=>"three"
    );


    $values = json_encode($post);

    $session = curl_init($request);
    curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt ($session, CURLOPT_POST,1);
    curl_setopt ($session, CURLOPT_POSTFIELDS, $values);

    // Tell curl not to return headers, but do return the response
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($session);
    curl_close($session);

    return json_decode($response);

Hope this helps.

Upvotes: 1

Related Questions