<turbo-stream action="update" target="wiki_3611_subpage_3642_info"><template>
<p>All response bodies are <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> or <a href="http://www.jsonapi.org" title="JSON API">JSON API</a> encoded.</p>
<p><strong>JSON:</strong></p>
<p>A single resource is represented as a <strong>JSON</strong> object:</p>
<pre class="card">{
    "field1": "value",
    "field2": true,
    "field3": []
}</pre>
<p>Below is a specific example of a single resource presented as a <strong>JSON</strong> object:</p>
<pre class="card">{
    "code": "CUST",
    "description": "Customer Transfers",
    "comments": []
  }</pre>
<p>A collection of resources is represented as a <strong>JSON</strong> array of objects:</p>
<pre class="card">[
    {
      "field1": "value",
      "field2": true,
      "field3": []
    },
    {
      "field1": "another value",
      "field2": false,
      "field3": []
    }
]</pre>
<p>Below a specific example of a collection of resources represented as a <strong>JSON</strong> array of objects:</p>
<pre class="card">[ 
  { 
    "code": "CUST01", 
    "description": "Customer Transfers", 
    "comments": [] 
  }, 
  { 
    "code": "CONTACT", 
    "description": "Contact Transfers", 
    "comments": 
      ["created_at": 2017-08-27T16:14:57Z", 
        "created_by": "xxname", 
        "comment": "updated transfer" }, 
        "created_at": 2017-08-28T15:19:34Z", 
        "created_by": "xxname", 
        "comment": "&lt;p&gt;adjusted&lt;/p&gt; - ready to run"} 
      ] 
  }, 
    "code": "Invoice" ,
    "description": "Invoice Transfers", 
    "comments":[] 
  }, 
  ... 
]
</pre>
<p><strong>JSON API:</strong></p>
<p>A single resource is represented as a <strong>JSON API</strong> object:</p>
<pre class="card">{
  "data":{ 
    "id": "1", 
    type": "groups", 
    "attributes": 
      { "code": "codename", 
        "description": "description of group" 
      }, 
      relationships":{ 
        "comments": {} 
      } 
    } 
  } 
}
</pre>
<p>Below a specific example of a single resource <strong>JSON API</strong> object:</p>
<pre class="card">{ 
  "data": 
    [{ 
      "id": "8", 
      "type": "groups", 
      "attributes": { 
        "code": "CUST01", 
        "description": "Customer Transfers" 
      }, 
      "relationships": { 
        "comments": { 
          "data": [] 
        } 
      } 
    }] 
  } 
}
</pre>
<p>A collection of resources is represented as a <strong>JSON API</strong> array of objects:</p>
<pre class="card">{ 
"data": 
  [ 
    { 
      "id":"1", 
      "type":"groups", 
      "attributes":{ //...this groups'attributes 
        "code": "codename", 
        "description": "description of group" 
      }, 
      "relationships": 
        { //...this groups'relationships 
          "comments": { 
            "data": [ 
              { 
                "id":"5", 
                "type":"comments" 
              }, 
              { 
                "id":"6", 
                "type":"comments" 
              } 
            ] 
          } 
        } 
    } 
  ], 
"included":  
  [ 
    { 
    "type": "comments", 
    "id": "5", 
    "attributes": { 
      "created_at": "yyyy-mm-ddThh:mm:ssZ", 
      "created_by": "author-name", 
      "comment": "text for the comment"} 
    }, 
    { 
      "type": "comments", 
      "id": "5", 
      "attributes": { 
        "created_at": "yyyy-mm-ddThh:mm:ssZ", 
        "created_by": "author-name", 
        "comment": "text for the comment" 
      } 
    } 
  ] 
}
</pre>

<p> </p>
<p>Below a specific example of a collection of resources represented as a <strong>JSON API</strong> array of objects:</p>
<pre class="card">{ 
  "data": [ 
    { 
      "id": "8", 
      "type": "groups", 
      "attributes": { 
        "code": "CUST01", 
        "description": "Customer Transfers" 
      }, 
      "relationships": { 
        "comments": { 
          "data": [ 
          ] 
        } 
      } 
    }, 
    { 
      "id": "2", 
      "type": "groups", 
      "attributes": { 
        "code": "CONTACT", 
        "description": "Contact Transfers" 
      }, 
      "relationships": { 
        "comments": { 
          "data": [ 
            { 
              "id": "10", 
              "type": "comments" 
            }, 
            { 
              "id": "12", 
              "type": "comments" 
            } 
          ] 
        } 
      } 
     }, 
     { 
      "id": "5", 
      "type": "groups", 
      "attributes": { 
        "code": "INVOICE", 
        "description": "Invoice Transfers" 
      }, 
      "relationships": { 
        "comments": { 
          "data": [ 
          ] 
        } 
       } 
     }, 
... 
], 
"included": [ 
  { 
    "id": "10", 
    "type": "comments", 
    "attributes": { 
      "created-at": "2017-08-27T16:14:57Z", 
      "created-by": "xxname", 
      "comment-text": "updated transfer" 
     } 
   }, 
   { 
    "id": "12", 
    "type": "comments", 
    "attributes": { 
      "created-at": "2017-08-28 15:19:34Z", 
      "created-by": "xxname", 
      "comment-text": "<p>adjusted</p> - ready to run" 
    } 
  } 
]
</pre>

<p>Timestamps are in <a href="http://en.wikipedia.org/wiki/Coordinated_Universal_Time">UTC</a> and formatted as <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO8601</a>.</p>
<p>Unset fields will be represented as a <code>null</code> instead of not being present. If the field is an array, it will be represented as an empty array - ie <code>[]</code>.</p>
<h4>HTTP Status Codes</h4>
<p>We use HTTP status codes to indicate the success or failure of a request.</p>
<p>Success codes:</p>
<ul>
<li><code>200 OK</code> - Request succeeded. Response included</li>
<li><code>201 Created</code> - Resource created.</li>
<li><code>204 No Content</code> - Request succeeded, but no response body.</li>
</ul>
<p>Error codes:</p>
<ul>
<li><code>400 Bad Request</code> - Could not parse the request</li>
<li><code>401 Unauthorized</code> - No authentication credentials (token) provided or authentication failed</li>
<li><code>403 Forbidden</code> - The Authenticated API user does not have access</li>
<li><code>404 Not Found</code> - Resource not found</li>
<li><code>415 Unsupported Media Type</code> - POST/PUT/PATCH request occurred without a <code>application/json</code> content-type</li>
<li><code>422 Unprocessable Entry</code> - A request to modify or create a resource failed due to a <a href="#validation-error">validation error</a></li>
<li><code>429 Too Many Requests</code> - Request rejected due to <a href="http://dev.enchant.com/api/v1#ratelimit">rate limiting</a> </li>
<li><code>500, 501, 502, 503, etc</code> - An internal server error occurred</li>
</ul>
<h4>Errors</h4>
<p>All 400 series errors (400, 401, 403, etc) will be returned with a <strong>JSON</strong> or <strong>JSON API</strong> object in the body and a <code>application/json</code>  or <code>application/vnd.api+json</code> content-type respectively.</p>
<pre><code>{ "message": "Not Found" }</code></pre>
<p>500 series error codes (500, 501, 502, etc) do not return <strong>JSON</strong> or <strong>JSON API</strong> bodies.</p>
<h4 id="validation-error">Validation Errors</h4>
<p>In case of validation errors on a POST/PUT/PATCH request, a <code>422 Unprocessable Entry</code> status code will be returned. The JSON response body will include an array of error messages.</p>
<pre class="card">{ 
  "message": "Validation Failed", 
  "errors": [ 
    { 
      "message": "Field is not valid" 
    }, 
    { 
      "message": "OtherField is already used" 
    } 
  ] 
}
</pre>
</template></turbo-stream>
<turbo-stream action="prepend" target="flash"><template></template></turbo-stream>
