5

Has anyone ever tried to use Atlassian's rest api from Mathematica? I'd love an example of a post request to create a ticket with specific properties.

With cURL, it's easy:

curl -D- -u [username] -p -X POST --data @[pathToJson] -H \"Content\
Type: application/json\" https://[YourCompany].atlassian.net/rest/api/2/issue/

But I'm not sure how to do it with URLFetch.

M.R.
  • 31,425
  • 8
  • 90
  • 281
  • After scanning your link it looks like it should be straight forward using URLFetch. You would have to use your JSON data in the "Body" option. What have you tried so far? (related REST API usage examples: http://mathematica.stackexchange.com/questions/40731/graph-databases-nosql-with-mathematica) – Mike Honeychurch Jan 06 '16 at 00:15
  • You can create the JSON data as nested lists of rules and then use ExportString to convert to JSON then add that to URLFetch. I don't have access to a JIRA to try but can write a conceptual answer for you to try if you like. I'll be away for 4-5 hours but when I get back – Mike Honeychurch Jan 06 '16 at 00:20
  • @MikeHoneychurch thanks! – M.R. Jan 06 '16 at 05:13

1 Answers1

5

Using the first example in the link to the JIRA API.

create your data in Mathematica:

data = {"fields" -> {
    "project" -> {"key" -> "TEST"},
    "summary" -> "REST ye merry gentlemen.", 
    "description" -> 
     "Creating of an issue using project keys and issue type names
using the REST API", "issuetype" -> {"name" -> "Bug"}
    }
  }

convert the Mathematica data to JSON:

jsondata = ExportString[data, "JSON"]

(* {
    "fields": {
        "project": {
            "key": "TEST"
        },
        "summary": "REST ye merry gentlemen.",
        "description": "Creating of an issue using project keys and issue type names using the REST API",
        "issuetype": {
            "name": "Bug"
        }
    }
} *)

Now post the data to the URL:

URLFetch["http://localhost:8090/rest/api/2/issue/",
 "Method" -> "POST",
 "Headers" -> {"Content-Type" -> "application/json"},
 "Username" -> "fred",
 "Password" -> "fred",
 "Body" -> jsondata
 ]
Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158
  • Authentication works, and I set my project key correctly, but I'm getting this error message: "{"errorMessages":[],"errors":{"summary":"Field 'summary'
    cannot be set. It is not on the appropriate screen, or
    unknown.","description":"Field 'description' cannot be set. It is
    not on the appropriate screen, or unknown."}}"
    – M.R. Jan 06 '16 at 22:18
  • I looked this up in a jira forum, they say: "The HTTP Basic Session Cookie just need to be preemptive: httpRequest.addHeader(new BasicScheme().authenticate(new UsernamePasswordCredentials("username","password"), httpRequest)); Jira says its a JSON format problem, but it is a auth-error. Hope it helps other people." – M.R. Jan 06 '16 at 22:19
  • @M.R. did you run the example in the answer or did you sub your own data in? It looks like the later and if that is the case you may be using fields that are not spelled correctly or do not exist. Can you confirm that the API examples work? – Mike Honeychurch Jan 06 '16 at 22:22
  • @M.R. you can use cookies with URLFetch. see the docs examples. – Mike Honeychurch Jan 06 '16 at 22:23
  • I ran the same data only switching the key, everything else works with curl the fields "summary", "description", "issuetype" are all standard and work when I use the @[json file path] in the curl command. Doing a RunProcess@StringSplit on the curl command string works, it's an issue with URLFetch, and I don't know how to use the cookies option here either. – M.R. Jan 07 '16 at 03:40
  • Actually scratch that, doing a RunProcess@StringSplit on the curl command string doesn't work either, I get an <|"ExitCode" -> 0, "StandardOutput" -> "HTTP/1.1 415 Unsupported Media Type"...|> – M.R. Jan 07 '16 at 03:46
  • Only guess but looks like a problem with your JSON. Why not run the example without any changes (other than username and password)? Also it is more useful to use ReadList or Import than RunProcess if you want to go down that path. – Mike Honeychurch Jan 07 '16 at 04:04
  • I think it's a problem with how Mathematica imports the Json, because the exact same Json file works with curl when references by file path. any other ideas? – M.R. Jan 07 '16 at 21:56
  • In my answer there is no JSON file. Why not just try and test my answer with your username and password before trying other things? I have not encountered any problems with ExportString to JSON and have been using this with REST APIs for years. Since I do not have JIRA and do not know what you are doing there is nothing more I can suggest – Mike Honeychurch Jan 07 '16 at 22:00