【发布时间】:2015-12-01 21:48:50
【问题描述】:
我正在尝试使用 Symfony2 和 FOSRestBundle 构建 API 的 POST。以下功能测试返回 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);
}
但是当我尝试通过 Curl 发送具有相同请求正文的请求时,它给了我一个 400 invalid json 消息:
{"code":400,"message":"收到无效的 json 消息"}
这是我尝试过的 curl 命令:
curl -X POST -d '{"articleContent":"title1"}' http://localhost:8000/api/v1/articles --header "内容类型:应用程序/json"
curl -X POST -d '{"articleContent":"title1"}' http://localhost:8000/api/v1/articles.json
请注意,GET 会返回给我一个 json,例如:
{"id":68,"article_content":"contents 内容"}
但在我的学说映射文件中,我的字段是 articleContent。我错过了什么?
非常感谢您的帮助。
【问题讨论】:
标签: php json symfony post curl