0

我想在 Guzzle 中进行自定义序列化。

我正在设置一个POST application/json请求,但我的对象在开头使用其名称(professionalSession)进行序列化,即:

{
 professionalSession :
  {
    param1 : "asdf",
    param2 : "jkl;",
    ...
   }
}

这与我试图调用的 REST API 不一致。(className 作为参数之一隐藏)。

这是我的定义serviceDescription.json

"PostAuthentication": {
        "httpMethod": "POST",
        "uri": "/xxx-person-service/session",
        "summary": "Posts the session object",
        "type": "json",
        "responseClass": "XXX\\WebServicesClientBundle\\Entity\\ProfessionalSession",
        "parameters":{
            "session": {
                "location": "json",
                "required": true
            },
            "session-identifier": {
                "location": "header",
                "required": true,
                "sentAs": "HTTP_X_SESSION_KEY"
            }
        }
    }

我想使用serviceDescription.json并且只覆盖它的 1 个参数(通过自己生成 json)。

我尝试将 param 的位置更改为body(正如在某处所说的那样),但 Content-Type 未正确设置为application/json.

我该怎么做?谢谢!

4

1 回答 1

1

I will reply as there are no responses yet and I've overcame this problem. Changing the location of param to body was good approach as it removes that top level JSON element. (This was signaled as a problem, but still - this is how Guzzle behaves.)

To change request to application/json you can use following description in serviceDescription.json:

   "PostAuthentication": {
        "httpMethod": "POST",
        "uri": "/xxx-person-service/session",
        "summary": "Posts the session object",
        "responseClass": "XXX\\WebServicesClientBundle\\Entity\\ProfessionalSession",
        "parameters":{
            "session": {
                 "location": "body",
                 "required": true
            },
            "session-identifier": {
                "location": "header",
                "required": true,
                "sentAs": "HTTP_X_SESSION_KEY"
            },
            //THIS is what you need:
            "content-type": {
                "location": "header",
                "static": true,
                "required" : true,
                "default" : "application/json",
                "sentAs" : "Content-Type"
            }
        }
    },
于 2013-11-11T16:32:24.110 回答