L Eder
L Eder

Reputation: 1

JSON string gets parsed correctly in Postman but the same fails in JavaScript

This is the JSON string i am passing to a POST request:

  var data =    {
          "location": {
            "value": {
              "href": "http://localhost:8080/restful/objects/businessentities.Location/47"
            }
          },
          "asset": {
            "value": {
              "href": "http://localhost:8080/restful/objects/businessentities.Asset/36"
            }
          }
  }

The AngularJS resource code is:

var AuditItemCreate = $resource(
  apiUrl +   '/restful/services/domainapp.dom.BusinessEntities.AuditItemMenu/actions/create/invoke/');

var a2 = new AuditItemCreate();
a2.$save(data)
     .then(function(res)  { console.log("success") })
     .catch(function(req) { console.log("error saving obj: " + JSON.stringify(req, null, 4)); })

However i am getting a server response 422 (Unprocessable Entity) error:

error saving obj: {
    "data": {
        "asset": {
            "value": "{\"value\":{\"href\":\"http://localhost:8080/restful/objects/businessentities.Asset/36\"}}",
            "invalidReason": "Expected a link (because this object's type is not a value) but found no 'href'"
        },
        "location": {
            "value": "{\"value\":{\"href\":\"http://localhost:8080/restful/objects/businessentities.Location/47\"}}",
            "invalidReason": "Expected a link (because this object's type is not a value) but found no 'href'"
        },
        "x-ro-invalidReason": "Mandatory"
    },

That same JSON string works successfully when used in Postman.

Upvotes: 0

Views: 149

Answers (1)

L Eder
L Eder

Reputation: 1

I was able to solve this issue -- by using the own resource class, rather its resource instance.

Upvotes: 0

Related Questions