user3712353
user3712353

Reputation: 4209

Loopback REST connector POST

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.

Upvotes: 1

Views: 2224

Answers (1)

ashwinik001
ashwinik001

Reputation: 5183

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.

Upvotes: 2

Related Questions