Support both application/hal+json and application/json

293 views
Skip to first unread message

Marcus Fulbright

unread,
Aug 18, 2014, 6:28:14 PM8/18/14
to resting-wi...@googlegroups.com
I'm currently working on a symfony2 project that heavily leverages the following bundles
  • FOS Rest
  • JMS Serializer
  • Bazinga HATEOAS bundle
These bundles seem to play together really well. However the HATEOAS bundle uses an event listener to add the _links stuff to standard json. Best I can tell, this means that applications can only support hal+json and hal+xml or plain old json and xml. But not all 4. I'm trying to find a way that will allow me to send back hal+json response when the request's accept headers call for it, but still have the ability to send back a plain old json response if the accept headers ask for it. It seems like this should be possible, however I cannot figure it out. 

Lukas Smith

unread,
Aug 18, 2014, 6:58:50 PM8/18/14
to Marcus Fulbright, resting-wi...@googlegroups.com
not sure I understand .. you are concerned you cannot return json/xml without HAL links if the client does not request HAL links?

regards,
Lukas Kahwe Smith

Marcus Fulbright

unread,
Aug 18, 2014, 7:13:09 PM8/18/14
to resting-wi...@googlegroups.com, fulbrigh...@gmail.com
Correct. The way I have things set up in my test bundle, I can tell that I cannot get json / xml back without the HAL links. When JMS Serializer is told to serialize json, it automatically puts in the HAL links because of the way the event listener in the HATAEOS bundle works.

Lukas Smith

unread,
Aug 19, 2014, 4:24:04 AM8/19/14
to Marcus Fulbright, resting-wi...@googlegroups.com

> On 19 Aug 2014, at 01:13, Marcus Fulbright <fulbrigh...@gmail.com> wrote:
>
> Correct. The way I have things set up in my test bundle, I can tell that I cannot get json / xml back without the HAL links. When JMS Serializer is told to serialize json, it automatically puts in the HAL links because of the way the event listener in the HATAEOS bundle works.

it is also possible to define the mappings with PHP code, so this might make it possible to define them dynamically. other option could be to try and see if you can unregister some handlers dynamically. it is not a use case I have come across yet.

regards,
Lukas

Marcus Fulbright

unread,
Aug 19, 2014, 3:32:21 PM8/19/14
to resting-wi...@googlegroups.com, fulbrigh...@gmail.com
So I have figured everything out. Ultimately, I should probably make a demo repo to share. Short version:
  • for FOSRest in my config.yml, set hal+json and hal+xml to true and defined mime types.
  • in my services.yml (inside my symfony bundle), defined my own handler service with the fos_rest.view_handler.default as a parent and called registerHandler twice, once for hal+json and againf or hal+xml
  • extended the HATEOAS json and xml event subscribers. Changed getsubscribedEvents() to return hal+json(xml) and in my config.yml I set the hateoas.event_subscriber.json(xml).class parameter to point to my extended versions.
  • I also had to declare JMS serializer services to handle hal+json(xml). I just used the default json and xml visitors in the service definition. Just changed for the format (again hal+json(xml))
If you do all of this, the following format listener will work: 
    format_listener:
        rules:
            - { path: '^/', priorities: ['hal+json', 'hal+xml', 'json', 'xml'], fallback_format: hal+json, prefer_extension: true }

This approach teaches both FOS Rest and JMS serializer to treat the HAL formats as unique content types and triggers the HATEOAS bundle to only work for hal content types which leaves vanilla json and xml alone. Again, I should probably set up a demo repo to illustrate everything. If anybody has any questions before I get around to that, please feel free to ask!

Di majo

unread,
May 8, 2024, 10:41:17 AM5/8/24
to RESTing with Symfony
MT103/202 DIRECT WIRE TRANSFER
PAYPAL TRANSFER
CASHAPP TRANSFER
ZELLE TRANSFER
LOAN DEAL
TRANSFER WISE
WESTERN UNION TRANSFER
BITCOIN FLASHING
BANK ACCOUNT LOADING/FLASHING
IBAN TO IBAN TRANSFER
MONEYGRAM TRANSFER
IPIP/DTC
SLBC PROVIDER
CREDIT CARD TOP UP
DUMPS/ PINS
SEPA TRANSFER
WIRE TRANSFER
BITCOIN TOP UP
GLOBALPAY INC US
SKRILL USA
UNIONPAY RECEIVER

Thanks.


NOTE; ONLY SERIOUS / RELIABLE RECEIVERS CAN CONTACT.

DM ME ON WHATSAPP
+44 7529 555638

Reply all
Reply to author
Forward
0 new messages