Adib Aroui
Adib Aroui

Reputation: 5067

POST json returns 400 with curl but functional test working

I am trying to build the POST of an API using Symfony2 and FOSRestBundle. The following functional test returns OK.

 public function testPostArticleAction(){

        $this->client->request(
                'POST',
                '/api/v1/articles.json',
                array(),
                array(),
                array('CONTENT_TYPE' => 'application/json'),
                '{"articleContent":"The content of the content"}'
                );
        $response = $this->client->getResponse();

        $this->assertJsonResponse($response,201, false); 
    }

But when I try to send a request via Curl with the same request body, it gives me a 400 invalid json message:

{"code":400,"message":"Invalid json message received"}

Here are the curl commands I have tried:

curl -X POST -d '{"articleContent":"title1"}' http://localhost:8000/api/v1/articles --header "Content-type:application/json"

curl -X POST -d '{"articleContent":"title1"}' http://localhost:8000/api/v1/articles.json

Please to note that the GET returns to me a json like:

{"id":68,"article_content":"contents contents"}

But my field is articleContent in my doctrine mapping file. What am I missing? Your help is much appreciated.

Upvotes: 0

Views: 898

Answers (1)

curtis1000
curtis1000

Reputation: 116

Try updating your header option to:

"Content-Type: application/json"

Additionally - does this request require any type of authentication? You'd need to pass in an auth cookie if so.

Upvotes: 1

Related Questions