4
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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: mathematica.stackexchange.com/questions/40731/…) $\endgroup$ – Mike Honeychurch Jan 6 '16 at 0:15
  • $\begingroup$ 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 $\endgroup$ – Mike Honeychurch Jan 6 '16 at 0:20
  • $\begingroup$ @MikeHoneychurch thanks! $\endgroup$ – M.R. Jan 6 '16 at 5:13
5
$\begingroup$

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
 ]
$\endgroup$
  • $\begingroup$ 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.\"}}" $\endgroup$ – M.R. Jan 6 '16 at 22:18
  • $\begingroup$ 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." $\endgroup$ – M.R. Jan 6 '16 at 22:19
  • $\begingroup$ @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? $\endgroup$ – Mike Honeychurch Jan 6 '16 at 22:22
  • $\begingroup$ @M.R. you can use cookies with URLFetch. see the docs examples. $\endgroup$ – Mike Honeychurch Jan 6 '16 at 22:23
  • $\begingroup$ 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. $\endgroup$ – M.R. Jan 7 '16 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.