Tran Triet Tran Triet - 1 month ago 16
reST (reStructuredText) Question

REST api and CURL

I am new to REST development and am writing a REST client on android to communicate with Bitbucket API using Retrofit2.0

I thought communicating with server API means sending request in the form

@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
@Path("owner") String owner,
@Path("repo") String repo
);


So I think the API document must provide the necessary information about endpoints and parameters and so on.

But when I take a look at Bitbucket's API, all I get was the instruction on how to use cURL to do something, like this instruction - for creating a new repository.

As far as I'm concerned, cURL is a Linux library and helps to facilitate request to API right from Linux terminal.

My questions are:


  • How is cURL and REST connected to each other?

  • How do I use and implement cURL in my Rest client application?

  • With the document provided by Bitbucket, what is the right way to implement a request to make the server create a new repository?


Answer

cURL is not REST. One is a tool, the other is a style of software architecture. You can use cURL to perform REST calls from the command line, but you do not need to implement it in your client application since there are likely tools available. Retrofit (actually just OkHttp) is one example.

Firstly, see curl -X POST? That means you need a POST request, not a @GET as you have in the code.

Secondly, that link you provided doesn't list contributors as an endpoint to the API.

Moving on, you need some JSON object that can represent this object to POST.

{
    "scm": "git",
    "project": {
        "key": "{ba516952-992a-4c2d-acbd-17d502922f96}"
    }
}

You can use Gson to create a Java Object that represents this data. Something like.

class Repo {
    String scm;
    Project project;
}

class Project {
    String key;
}

Then, I recommend remove the leading / from the annotation URL, as that needs to be appended to the base url.

@POST("repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
        @Path("owner") String owner,
        @Path("repo") String repo,
        @Body Repo data
);
Comments