简体   繁体   中英

POST request to JIRA REST API on a remote machine

How do I talk to the REST API of a JIRA installation that runs on a remote server?

Details

I'd like to work through this little example of how to interact with JIRA's REST API.

This is the request I'd like to issue:

curl -u admin:admin -X POST --data @data.txt -H "Content-Type: application/json" http://localhost:2990/jira/rest/api/2/issue/

My JIRA application runs on an AWS machine with, say, the following URL:

http://aws-url:8080

This is the R call I used for sending the POST request:

httr::POST("http://aws-url:8080/jira/rest/api/2/issue/", 
  "-u" = "myuser:mypassword", 
  "--data" = "@data.json", 
  "-H" = "Content-Type: application/json")

However, I get the following 404 response:

Response [http://aws-url:8080/jira/rest/api/2/issue/]
  Date: 2016-07-15 14:28
  Status: 404
  Content-Type: text/html;charset=UTF-8
  Size: 2.76 kB
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-C...


<ul class="atlassian-footer">
    <li>
        Atlassian JIRA <a class="seo-link" rel="nofollow" href="https://www.atlassi...



        <span id="footer-build-information">(v7.1.9#71013-<span title='1aa0586d78ef...
...

So I'm guessing I either misspecified the URL (or am still misunderstanding how the REST API of my JIRA instance needs to be contacted) and/or misspecified the call to POST ?

Sorry, not really familiar with the whole web technology stack yet. The question is related to this one

Update

Tried this request instead, but got the same result:

httr::POST(url,
  body = "-u myuser:mypassword --data @data.json",
  encode = "json"
)

404 means "not found", so there's simply something wrong with url you use.

What do you get if you paste something like this in a browser?

http://aws-url:8080/jira/rest/api/2/issue/{anExistingIssueKey}

Also if you access JIRA through the browser, do you use http://aws-url:8080/jira or does it have something else than /jira?

URL structure

Seems like the actual URL needs to be like this:

http://aws-url:8080/rest/api/2/issue/

or

http://aws-url:8080/rest/api/latest/issue/

instead of

http://aws-url:8080/jira/rest/api/latest/issue/

assuming that your JIRA instance runs on port 8080

I'm not quite sure why it's okay to drop the context (which would be jira in my case) from the URL as this resource suggests the URL structure needs to be like this:

http://host:port/context/rest/api-name/api-version/resource-name

The only explanation I have for that as a web-dev n00b is that I'm already explicitly adressing the JIRA instance by it's port number somehow? I might however be mixing up the meaning of context and api-name as well (not that deep into that stuff yet ;-)). Could it be that api-name gets a default value if omitted?

GET request

This GET request

res <- GET(http://aws-url:8080/rest/api/latest/issue/{anExistingIssueKey},
    authenticate("user", "password"))

gives me a 200 response with all the issue details of an issue that already existed.

POST request

The actual POST request needs to look like this:

url <- "http://aws-url:8080/rest/api/latest/issue/"
res <- POST(url,
  authenticate("user", "password"),
  body = upload_file("data.txt"),
  encode = "json"
)

and gives you a 201 response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM