PolymerElements / iron-ajax

Easily make ajax requests
https://www.webcomponents.org/element/PolymerElements/iron-ajax
127 stars 113 forks source link

Support for +json media type suffix #294

Open mmartensson opened 6 years ago

mmartensson commented 6 years ago

Description

Specifying a Content-Type header of application/vnd.foo.bar-v1+json and setting the body of an iron-ajax to an object is not supported.

As per https://tools.ietf.org/html/rfc6839 section 3:

[RFC4627] defines the "application/json" media type. The suffix "+json" MAY be used with any media type whose representation follows that established for "application/json".

Reasons for using a +json suffixed media type include versioning of REST calls and resources that can handle variations of request payloads.

Expected outcome

The data posted to the server is the object having passed through JSON.stringify().

Actual outcome

The data posted to the server is [object Object].

Live Demo

https://jsbin.com/necokalogu/edit?html,output

Steps to reproduce

  1. Create an iron-ajax for a resource expecting content in the request (typically a PUT/POST).
  2. Set the body to be a javascript object.
  3. Set the contentType property, or the Content-Type header, to application/vnd.foo.bar-v1+json.
  4. Send the request.

Source code reference

The _encodeBodyObject() of iron-request.html contains a switch/case that specifically checks for application/json and application/x-www-form-urlencoded. I suppose an .endsWith("+json") is a reasonable approach, but [object Object] is really never a good fallback.

phidias51 commented 6 years ago

I tried using application/hal+json for a restful request but the content type was still set to application/json in the GET request.