0

I am developing a polymer app and I want to make a call to the RestApi. this is the how the request body is

{
    "languageId": Presently English is the only supported language. Always 1, 
    "productCode":"Medicus",
    "timeZoneName":"Time zone name of device. For e.g. Asia/Calcutta",
    "timeZoneOffset": Time zone offset from UTC in milliseconds. For e.g. IST = 19800000,
    "user":{
     "firstName":"First name of the user",
     "lastName":"Last name of the user",
     "middleName":"Middle name of the user",
     "password":"Password provided by the user",
     "userTypeId":2 = Doctor, 3 = User,
     "fields":[
         {
            "Id":1,
            "values":["Mobile number provided by the user”]
         }
     ]
    }
}

i am not getting the proper idea of how i should specify these parameters in the params='{}' of iron-ajax element.

aries12
  • 370
  • 2
  • 18
  • For starters, your JSON is invalid. See http://jsonlint.com and paste your code there; you will see what's invalid. – Geoff James Oct 20 '16 at 08:32

1 Answers1

0

Put something like this in your template (I am assuming POST to your rest API, since you said body in your question. If its GET replace body= with params=

<iron-ajax
       id="fetchday"
       url="/api/fetchday"
       handle-as="json"
       content-type="application/json"
       method="POST"
       body="[[params]]"
       last-response="{{results}}"
       on-response="_gotData"
       on-error="_error"></iron-ajax>

And in your polymer element properties

Polymer({
  is: 'my-element'
  properties: {
    params: {
     type: Object
    } 
  },
  _someFunction: function() {
    this.params = //ASSIGN YOUR JSON OBJECT TO PARAMS HERE
    this.$.fetchday.generateRequest();
  },
  _gotData: function(e) {
    //response data is in both this.results and e.detail.response
 },
 _error: function() {
 }
});
akc42
  • 4,893
  • 5
  • 41
  • 60
  • but now i am getting 502 proxy error do i need to perform JSON.stringify on params and then send it to body or will it do itself considering the content type – aries12 Oct 20 '16 at 11:07
  • You provide a Javascript Object to this.params in above example. I think its the `handle-as="json"` that tells iron ajax how to encode it in the body – akc42 Oct 20 '16 at 15:31
  • is it not possible to make a RestApi call from localhost?? – aries12 Oct 21 '16 at 04:41
  • thank you @akc42 for the answer the problem was with the json format its resolved. – aries12 Oct 21 '16 at 09:35