Question

I would like to have a custom serialization in Guzzle.

I'm setting a POST application/json request, but my object is serialized with its name (professionalSession) at the beginning, i.e.:

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

That is inconsistent with the REST API I'm trying to call. (className is hidden as one of the parameters).

This is my definition in 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"
            }
        }
    }

I would like to use serviceDescription.json and only override its 1 parameter (by produce json myself).

I tried changing the location of param to body (as it was said in SO somewhere), but Content-Type is not being properly set to application/json.

How can I do it? Thanks!

Was it helpful?

Solution

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"
            }
        }
    },
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top