RESTful self-determination api

I am creating a (RESTful) api (using HTTP) that I want to use with javascript.

I find my own composition in javascript for example

function getPost(id)
{
    $.getJSON("/api/post/" + id, function(data){
        // Success!
    });
}

There should be a smarter way than hardcoding api in javascript, maybe something like asking the api itself for what the getPost URL should look like?

function getApiPostUrl()
{
    $.getJSON("/api/getpost/");
}

returns something like

url: "/api/post/:id"

Which can be parsed by javascript to get the url to receive the message with id =: id. I like this approach.

Is this the standard way to do this? I am looking for a good approach, so I don’t need to invent all this if there is a good solution.

+4
source share
3 answers

, , RESTful API URI - , . , - , API.

, , API http://fqdn/api/posts, , :

[ "http://fqdn/api/posts/1",
  "http://fqdn/api/posts/2",
  "http://fqdn/api/posts/3" ]

javascript , . . HATEOAS, API .

, ( UML, ), , , API. , , .

+2

, . , , - "API ", API , , , ( API).

Mason, :

{
  id: 1234,
  title: "This is issue no. 1",
  "@link-templates": {
    "is:issue-query": {
      "template": "http://issue-tracker.org/mason-demo/issues-query?text={text}&severity={severity}&project={pid}",
      "title": "Search for issues",
      "description": "This is a simple search that do not check attachments.",
      "parameters": [
        {
          "name": "text",
          "description": "Substring search for text in title and description"
        },
        {
          "name": "severity",
          "description": "Issue severity (exact value, 1..5)"
        },
        {
          "name": "pid",
          "description": "Project ID"
        }
      ]
    }
  }
}

URL- RFC 6570.

API . HAL, Sirene, Collection-JSON Hydra.

.

+1

(abbr. HATEOAS) REST.

HATEOAS, , , API. URL. URL-, HTTP, , , , ..

, , .. . , API- , , , URL-.

URLs are always API-specific; semantics are optional; therefore, these restrictions separate the client from the API. After that, the client will not break the URL changes or even change the data structure, since it will use the standard hypermedia format (for example, HAL, JSON-LD, ATOM or even HTML) and semantics (possibly RDF) to analyze the response of the body.

+1
source

All Articles