Farran Medhurst
Farran Medhurst

Reputation: 31

Issue with Body Argument for POST request

I am attempting to create a new team in our instance of Azure Devops through the Devops REST API. This is being done through R with the package 'httr' for the POST request and 'jsonlite' for the toJSON function.

I have been utilising the documentation on Microsoft (https://learn.microsoft.com/en-us/rest/api/azure/devops/core/teams/create?view=azure-devops-rest-5.1) to structure the request correctly but keep getting a 400 error (Bad Request).

I am an administrator for the group so have permissions to create teams and the url is correct as I can return data with a GET request.

For the body argument I am using the following code;

    args <- list(name = "my new team")
    body <- toJSON(args, auto_unbox = TRUE)

Printing 'body' to the console returns

    {"name":"my new team"}

which looks consistent with the JSON request body in the Microsoft documentation.

The code for the POST request is below;

    create.task <- POST(paste0("https://dev.azure.com/",org.id,"/_apis/projects/",
                   project.id,"/teams?api-version=5.1"), 
                   encode = "json", 
                   authenticate(username, token, type = "basic"), 
                   body = body,
                   verbose())

This will return the following error message rather than creating the new Team in Devops.

    "HTTP/1.1 400 Bad Request
    <- Cache-Control: no-cache
    <- Pragma: no-cache
    <- Content-Length: 446
    <- Content-Type: application/json; charset=utf-8
    <- Expires: -1"

Unfortunately, this is not reproduce-able but I wanted to see if there is an obvious error that I am making.

Thanks.

Upvotes: 1

Views: 744

Answers (2)

Hugh Lin
Hugh Lin

Reputation: 19451

I don't think it is necessarily a problem with your Authorization. I tested this rest api in Postman and authenticated with PAT. The result of the test is that the team can be created successfully.

enter image description here enter image description here

Upvotes: 3

Farran Medhurst
Farran Medhurst

Reputation: 31

Thanks to Hugh Lin for the suggestion to go through Postman. From this I got my authorization token and managed to get my request to work. I then exported the code snippet in cURL and used https://curl.trillworks.com/#r to convert the curl command to R httr.

Another difference to my early approach is that I set the Authorization, Host and Content Type with add_headers, and the api-version in the query.

The code below is what I used;

require(httr)

headers = c(
  `Authorization` = 'Basic XXXXXXXXXXXXXXXX=',
  `Content-Type` = 'application/json',
  `Host` = 'dev.azure.com'
)

params = list(
  `api-version` = '5.1'
)

data <- toJSON(list(name = "My New Team"), auto_unbox = TRUE, pretty = TRUE)

res <- httr::POST(url = 'https://dev.azure.com/{organization}/_apis/projects/{projectId}/teams',
                  httr::add_headers(.headers=headers), 
                  query = params, 
                  body = data)

This successfully created the new team.

> status_code(res)
[1] 201

Upvotes: 2

Related Questions