0
votes

I tried to create a model, and connect to my API test server.

Here's the REST datasource configuration :

"postsREST": {
"name": "postsREST",
"connector": "rest",
"operations": [{
    "template": {
        "method": "GET",
        "url": "http://localhost:3001/posts"
    },
    "functions": {
        "find": []
    }
}, {
    "template": {
        "method": "POST",
        "url": "http://localhost:3001/posts",
        "headers": {
            "accept": "application/json",
            "content-type": "application/json"
        },
        "query": {
            "title": "{^title}",
            "author": "{^author}"
        },
        "body": {
            "title": "{^title}",
            "author": "{^author}"
        }
    },
    "functions": {
        "create": [
            "title",
            "author"
        ]
    }
}]

}

The problem is, that when I use the explorer, the generated request url is this:

http://localhost:3000/api/posts/create?title=f&author=f

Instead of:

http://localhost:3000/api/posts

What am I doing wrong? Maybe there is new documentation?

Thanks.

1
I noticed that you are using port 3001 (localhost:3001) in your file and port 3000 in your browser, is it a typo ? - Overdrivr
The REST datasource connector is intended to be used by your models to access an external API... this appears to be accessing the same model in a circular fashion. What exactly are you trying to accomplish? - Jordan Kasper
Of course it will be external. Its just for example. The problen is that the post url behaves like a get, as you can see from the code. The url is the local (port 3000),and the 3001 is the external. - user3712353

1 Answers

2
votes

You should use form instead of req or body, if you want the params not to be part of the request-url. req or body will append your parameters to the request URL.

Using form will send your parameters just like a form is submitted using POST method and hence as part of the request body.

So, try the following way for the template section in your code:

"template": {
        "method": "POST",
        "url": "http://localhost:3001/posts",
        "headers": {
            "accept": "application/json",
            "content-type": "application/json"
        },
        "form": {
            "title": "{^title}",
            "author": "{^author}"
        }
    },

Additionally, I do not see any point in adding the same parameters to the req and body attributes both.