I am new to REST development and am writing a REST client to communicate with the Bitbucket API using Retrofit2.0
I thought communicating with the server API means sending a request in the following form
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
@Path("owner") String owner,
@Path("repo") String repo
);
I thought the API documentation should provide the necessary information about endpoints and parameters and so on.
But when I take a look at Bitbucket's API, all I got 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 a request to the API right from a Linux terminal.
My questions are:
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 libraries such as Jackson, Gson, Moshi, etc. to create a Java class 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
);