Question T10960
Visible to All Users

Load Dropdown choices with POST method API with Survey Creator in SurveyJS

created 2 years ago

Hi,
I have similar issue. i would like to use the dropdown question's choicesByUrl property. the rest api(actually graphql api) supports only POST method and need to pass the query as request body.but i see that the current question supports only GET method
we need something like below.

JSON
{ type: "dropdown", name: "country", title: "Select the country...", isRequired: true, choicesByUrl: { "url": "https://surveyjs.io/api/CountriesExample", "method": "post", "body" :{ "query:{} } "valueName": "name" } }

do you have any suggestions?
Thanks
Azeet

Answers approved by surveyjs Support

created 2 years ago

Hello Azeet,
We did not expect that somebody needs to use "POST" method here. We do not support it.
You can execute your call to the server manually and then set your question.choices array.

Thank you,
Andrew
SurveyJS Team

    Comments (2)
    AC AC
    Azeet Chebrolu 2 years ago

      Thank you. i have managed to enable GET in my API but i have to pass some querystring parameters.i dont see the AJAX call is being made with below configuration and see an empty list in my dropdown.
      i can call this same end point using postman successfully.any idea?

      JSON
      { "title": "empty survey", "description": "empty survey", "pages": [ { "name": "page1", "elements": [ { "type": "dropdown", "name": "question3", "title": "Application Name", "choicesByUrl": { "url": "http://localhost:4000/graphql?query=query%20Query%20{%20allApplications%20{%20name%20shortName%20}}&operationName=Query", "path": "data.allApplications", "valueName": "shortName", "titleName": "name" } }, { "type": "text", "name": "question2" } ] } ] }
      AC AC
      Azeet Chebrolu 2 years ago

        nvm i think i missed urlencode of the query string.thanks.

        Other Answers

        created 2 years ago

        btw i had to provide path value to a multi level nested property and i took a while for me to figure out what value to give as seperator.
        its not . or ; . it turned out to be ,.it will be helpful if the documentation mentions that. i had to look at the souce code to figure that out.here is my final working version

        JSON
        { "title": "empty survey", "description": "empty survey", "pages": [ { "name": "page1", "elements": [ { "type": "dropdown", "name": "question3", "title": "Application Name", "choicesByUrl": { "url": "http://localhost:4000/graphql?query=query%20Query%20%7B%20allApplications%20%7B%20name%20shortName%20%7D%7D&operationName=Query", "path": "data,allApplications", "valueName": "shortName", "titleName": "name" } }, { "type": "text", "name": "question2" } ] } ] }
          Comments (3)

            Hello Azeet,
            Thank you for the update. The choicesRestful | getPathes processes the path argument value.

            JavaScript
            private getPathes(): Array<string> { var pathes = []; if (this.processedPath.indexOf(";") > -1) { pathes = this.path.split(";"); } else { pathes = this.processedPath.split(","); } if (pathes.length == 0) pathes.push(this.processedPath); return pathes; }

            You can see from this code that if path contains the semicolon ;, the getPathes function will split the entire path by using ;. Otherwise, the comma , delimiter will be used.

            I agree that we should add this information to our docs. Still, from what I gather, the ; delimiter didn't work for you, correct? Could you share an example of your API response?

            AC AC
            Azeet Chebrolu 2 years ago

              it looks like ; works as well. i am not sure what i did wrong then.
              but yes both ; and , works.

                Thank you for the confirmation, Azeet. I'm happy to hear that you sorted this out.

                We'll certainly update our docs in this regard.

                Please feel free to contact us if you have any questions.