Hi,
I am building a REST API client in Python for JIRA. I am trying to add a comment on an issue using an API call.
According to the documentation, the response from the API call would be 201 if the comment is added successfully.
It returns with 200 and shows existing comments but doesn’t add the new comment I am trying to add via API call.
What am I doing wrong?
Below is my simple python code:
def add_comment():
data ={"body":"this is a comment from rest API client"}
headers = {"Content-Type":"application/json"}
add_comment_response = requests.post("https://jira-euc-test.eng.com/rest/api/2/issue/GS-2/comment",data=json.dumps(data),headers=headers,auth=("dummyusername","dummypassword))
parsed_response = json.loads(add_comment_response.content)
print json.dumps(parsed_response,indent=4,sort_keys=True)
print add_comment_response.status_code
1 Like
Just looking at the API spec, could it be that you need to include a visibility
object?
1 Like
I have modified body of the API call to as mentioned in the API Spec.
data ={“body”:“this is a comment from rest API client”,“visibility”:{“type”:“role”,“value”:“Administrators”}}
Still gives the same 200 status code instead of 201.
Also tried the value of the role to invalid ones as well. Doesn’t throw an error. Just returns 200 and behaves as a GET request instead of POST.
Are there any permissions or user roles that could be the reason?
Thanks.
1 Like
Here’s an example of posting a comment to my development server (some details redacted):
http --auth david.pinn@projectbalm.com:<redacted> --json --pretty all POST https://projectbalmdev.atlassian.net/rest/api/2/issue/MOON-4/comment body="this is a comment from rest API client"
The http
command is from the https://httpie.org client, which is a curl-like command line utility. The raw HTTP request is shown below:
POST /rest/api/2/issue/MOON-4/comment HTTP/1.1
Host: projectbalmdev.atlassian.net
Content-Length: 50
Accept-Encoding: gzip, deflate
Accept: application/json
User-Agent: HTTPie/0.9.2
Connection: keep-alive
Content-Type: application/json
Authorization: Basic <redacted>
{"body": "this is a comment from rest API client"}
The response is shown below (some details redacted):
HTTP/1.1 201 Created
Server: nginx
Date: Sun, 02 Apr 2017 05:37:26 GMT
Content-Type: application/json;charset=UTF-8
X-AREQUESTID: 937x7285x1
X-AUSERNAME: david.pinn
X-ATENANT-ID: projectbalmdev.atlassian.net
Location: https://projectbalmdev.atlassian.net/rest/api/2/issue/10701/comment/10001
Cache-Control: no-cache, no-store, no-transform
X-Content-Type-Options: nosniff
Set-Cookie: JSESSIONID=<redacted>; Path=/; Secure; HttpOnly
Set-Cookie: atlassian.xsrf.token=<redacted>|lin; Path=/; Secure
Strict-Transport-Security: max-age=315360000;includeSubDomains
Transfer-Encoding: chunked
Connection: Keep-alive
{"self":"https://projectbalmdev.atlassian.net/rest/api/2/issue/10701/comment/10001","id":"10001","author":{"self":"https://projectbalmdev.atlassian.net/rest/api/2/user?username=david.pinn","name":"david.pinn","key":"admin","emailAddress":"david.pinn@projectbalm.com",
...}
So, it definitely works. I’d suggest using a debugging proxy, or maybe a verbose logger, to view the raw request and response, and determine where it differs from mine.
1 Like
Thanks for this. I will try to debug what’s happening. Initially, I thought it could be a problem with my python code, but I have tried the same POST call with Postman Client, cur command and got the same result.
Anyways, I will look into it and update what was I doing wrong. Thanks for the help.
1 Like
@david.pinn I have installed httpie and tried the same command as yours.
http --auth jadmin:**** --json --pretty all POST https://jira-euc-test.eng.****.com/rest/api/2/issue/RAC-1/comment body="this is comment from rest api"
Below is the response:
HTTP/1.1 302 Found
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 218
Content-Type: text/html; charset=iso-8859-1
Date: Sun, 02 Apr 2017 06:47:48 GMT
Keep-Alive: timeout=12, max=250
Location: https://jira-euc-test.eng.***.com/jira/rest/api/2/issue/RAC-1/comment
Server: Apache/2.2.15 (CentOS)
Vary: Accept-Encoding
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://jira-euc-test.eng.***.com/jira/rest/api/2/issue/RAC-1/comment">here</a>.</p>
</body></html>
I get 302 status code. Could this be the reason why I was getting 200 status code instead of 201 when I tried POST call from python script?
1 Like
That may have something to do with it, @kaushikveluru. Check out how to tell httpie to follow redirects.
1 Like
added --follow to my httpie command. Got the same 200 response code as earlier.
Can you try posting directly to https://jira-euc-test.eng.***.com/jira/rest/api/2/issue/RAC-1/comment
, which is the location to which your original request redirects?
My theory is that the redirect is sending a GET request to the second URL, which of course returns the comments with a 200 OK response.
1 Like
You’ve gone very quiet, @kaushikveluru. How are you getting on?
Hey David,
Sorry for the delay. Monday just started for me.
I have tried with https and also “–follow” that follows the location until the direct url. Both returned with status code 200
http --auth jadmin:***--json --pretty all POST https://jira-euc-test.eng.***.com/rest/api/2/issue/RAC-1/comment body="this is comment from rest api" --follow
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, no-transform
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: application/json;charset=UTF-8
Date: Mon, 03 Apr 2017 17:28:05 GMT
Keep-Alive: timeout=12, max=249
Server: Apache-Coyote/1.1
Path=/jira
Transfer-Encoding: chunked
Vary: Accept-Encoding,User-Agent
X-AREQUESTID: 808x50450x1
X-ASEN: SEN-L8111500
X-ASESSIONID: tl8v4f
X-AUSERNAME: jadmin
X-Content-Type-Options: nosniff
X-Seraph-LoginReason: OK
I have also tried to use POSTMAN Rest API client. GET and PUT calls work but not the POST.
I tried to add a comment via editing the issue. See that in the docs here
The PUT call using POSTMAN works fine. It adds a comment to the issue and returns 204.
Same PUT call returns 200 from python script. I have notified the same to my JIRA administrator. He’s looking into it.
I would like to know what’s happening behind the scenes.
I have also tried using curl commands.
curl -D- -u jadmin:*** -X POST --data "{\"body\": \"blah blah\"}" -H "Content-Type: application/json" -L https://jira-euc-test.eng.***.com/rest/api/2/issue/RAC-1/comment
Here is the response:
HTTP/1.1 302 Found
Date: Mon, 03 Apr 2017 17:44:22 GMT
Server: Apache/2.2.15 (CentOS)
Location: https://jira-euc-test.eng.***.com/jira/rest/api/2/issue/RAC-1/comment
Vary: Accept-Encoding
Content-Length: 256
Content-Type: text/html; charset=iso-8859-1
HTTP/1.1 400 Bad Request
Date: Mon, 03 Apr 2017 17:44:22 GMT
Server: Apache-Coyote/1.1
X-AREQUESTID: ***
X-ASEN: ***
X-Seraph-LoginReason: OK
X-ASESSIONID: ***
X-AUSERNAME: jadmin
Cache-Control: no-cache, no-store, no-transform
X-Content-Type-Options: nosniff
Content-Type: application/json;charset=UTF-8
Set-Cookie: JSESSIONID=***; Path=/jira; HttpOnly
Set-Cookie: atlassian.xsrf.token=***; Path=/jira
Vary: Accept-Encoding,User-Agent
Connection: close
Transfer-Encoding: chunked
{"errorMessages":["No content to map to Object due to end of input"]}
I have tried the data payload in different formats:
"{\"body\": \"blah blah\"}"
{"body": "blah blah"}
"body": "blah blah"
Got the same error. 400 bad request and the error message: No content to map to Object due to end of input
@david.pinn You were right. After a 302, it was sending a GET and that’s why it returned 400 bad request. Because GET won’t have a payload.
I got it working in curl. I had to add “–post302” to the command to avoid sending a GET when it hits a 302.
After adding “–post302”, I got the curl command working. I returned 201 and created a comment.
Thanks a lot for your help David. I appreciate it.
I just ran into this same problem. This post finally gave me the “aha” moment that it was a redirect. I was using postman, and I didn’t realize I was using http instead of https. When it redirected from http to https, it changed the request to a “GET” rather than a “POST”.