curl: (7) couldn't connect to host

Vaibhav Hajela April 9, 2015

We use JIRA OnDemand. We are trying to access JIRA rest APi to add a comment.
When we run following Curl command from our Linux Machine 

curl -D- -u <User>:<pass> -X -POST -H "Content-type: application/json"  -d"TEST" https://sungardvaldi.atlassian.net/rest/api/2/issue/<JIRA_ID>/comment

We get Error "Couldn't resolve host 'sungardvaldi.atlassian.net'

However, we can directly access the link through browser "https://sungardvaldi.atlassian.net/rest/api/2/issue/<JIRA_ID>/comment"

 So we tried using JavaScript to do this

var xmlHttpReq = false;
var self = this;
self.xmlHttpReq = new XMLHttpRequest();
self.xmlHttpReq.open("POST", "https://sungardvaldi.atlassian.net/rest/api/2/issue/<JIRA_ID>/comment", true);
self.xmlHttpReq.setRequestHeader("Content-Type", "application/json");
self.xmlHttpReq.setRequestHeader("Authorization", "Basic " + Base64.encode(<User> + ":" + <Pass>));
self.xmlHttpReq.send("{\"format\":\"json\",\"pattern\":\"#\"}");

When We run this script, we get error "Failed to load resource: the server responded with a status of 400 (Bad Request)"

Can anyone please assist, what are we doing wrong here and how can we achieve this?

Should we get IP and host of our JIRA from our Administrator and try the Curl again? 

6 answers

1 accepted

0 votes
Answer accepted
rsperafico
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 10, 2015

Hello Vaibhav,

Please, find bellow a tested implementation to post a comment to an issue (http://example.com:8080/jira/rest/api/2/issue/{issueIdOrKey}/comment [GETPOST]) in JIRA Cloud:

// Method POST
 
// [USERNAME], i.e.: myuser
// [PASSWORD], i.e.: mypassword
// [COMPANY], i.e.: sungardvaldi
// [ISSUE], i.e.: COMM-1

var postJSON = function(url, data, successHandler, errorHandler) {
  var xhr = typeof XMLHttpRequest != 'undefined'
    ? new XMLHttpRequest()
    : new ActiveXObject('Microsoft.XMLHTTP');
  xhr.open('post', url, true);
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.setRequestHeader("Authorization", "Basic " + btoa("[USERNAME]:[PASSWORD]"));
  xhr.onreadystatechange = function() {
    var status;
    var data;
    // https://xhr.spec.whatwg.org/#dom-xmlhttprequest-readystate
    if (xhr.readyState == 4) { // `DONE`
      status = xhr.status;
      if (status == 200) {
        data = JSON.parse(xhr.responseText);
        successHandler &amp;&amp; successHandler(data);
      } else {
        errorHandler &amp;&amp; errorHandler(status);
      }
    }
  };
  xhr.send(data);
};

postJSON('https://[COMPANY].atlassian.net/rest/api/2/issue/[ISSUE]/comment', 
'{"body":"My Comment from Javascript"}',
function(data){
	console.log(data);
}, function(status){
	console.log(status);
});

If you find this answer useful, I would kindly ask you to accept it so the same will be visible to others who might be facing the same issue you have inquired.

Thank you for your understanding.

Kind regards,
Rafael P. Sperafico
Atlassian Support

0 votes
Vaibhav Hajela April 10, 2015

Thanks Rafael,

We have web proxy and that is the reason we are able to access it from our web browser.

Lets focus on why we are not able to do it via JavaScript as the URLs are accessible through web browser.
var xmlHttpReq = false;
var self = this;
self.xmlHttpReq = new XMLHttpRequest();
self.xmlHttpReq.open("POST", "https://sungardvaldi.atlassian.net/rest/api/2/issue/<JIRA_ID>/comment", true);
self.xmlHttpReq.setRequestHeader("Content-Type", "application/json");
self.xmlHttpReq.setRequestHeader("Authorization", "Basic " + Base64.encode(<User> + ":" + <Pass>));

self.xmlHttpReq.send("{"body": "This is a comment regarding the quality of the response."}");

 When i ran this script on web browser,  i get error 400: Bad Request.  Are we missing something on JasonFormat data ?

When we ran the same script on another machine, we get error "403: Forbidden".

Can you please take a look.

 

0 votes
rsperafico
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 10, 2015

Hello Vaibhav,

It has come to my mind you might have a web proxy in place and this would explain the reason for being able to access https://sungardvaldi.atlassian.net from your web browser but not from Command Prompt / Terminal. Could you please verify with your network administrator this matter?

Kind regards,
Rafael P. Sperafico
Atlassian Support

0 votes
Vaibhav Hajela April 10, 2015

We also executed the given Curl command to get ServerInfo 
curl -D- -u [USERNAME] -p -H "Content-Type: application/json" -X GET https://[DOMAIN].atlassian.net/rest/api/2/serverInfo

Although, this curl also resulted into "Couldn't connect to host" but the link opened on browser.

But ServerInfo didn't give us IP/Port, it just gives the base Url "baseUrl":"https://sungardvaldi.atlassian.net".

Can you please also suggest why the below JavaScript results into "400: Bad Request" and how can we fix it.

 

We assume that since the link can be opened on browser, HTTP post through JavaScript should work.

var xmlHttpReq = false;
var self = this;
self.xmlHttpReq = new XMLHttpRequest();
self.xmlHttpReq.open("POST", "https://sungardvaldi.atlassian.net/rest/api/2/issue/<JIRA_ID>/comment", true);
self.xmlHttpReq.setRequestHeader("Content-Type", "application/json");
self.xmlHttpReq.setRequestHeader("Authorization", "Basic " + Base64.encode(<User> + ":" + <Pass>));

0 votes
Vaibhav Hajela April 10, 2015

HI Rafael,

I cant ping or telnet 'sungardvaldi.atlassian.net' from Linux mahine as well as  from Windows Desktop.

Please see the result below.

C:\Program Files (x86)\Google\Chrome\Application>ping sungardvaldi.atlassian.net

Pinging sungardvaldi.atlassian.net [165.254.226.66] with 32 bytes of data:
Request timed out.
Request timed out.

 

 

C:\Program Files (x86)\Google\Chrome\Application>telnet sungardvaldi.atlassian.n
et 443
Connecting To sungardvaldi.atlassian.net...Could not open connection to the host
, on port 443: Connect failed

However, we can directly access the link through browser "https://sungardvaldi.atlassian.net"

0 votes
rsperafico
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 9, 2015

Hello Vaibhav,

Thank you for your question.

Could you please run the following from Linux box and attach its response:

ping sungardvaldi.atlassian.net
telnet sungardvaldi.atlassian.net 443

The following command returns general information about the current JIRA server. Please, refer to http://example.com:8080/jira/rest/api/2/serverInfo [GET] for further information.

 

# [USERNAME], i.e.: myuser
# [DOMAIN], i.e.: mycompany
curl -D- -u [USERNAME] -p -H "Content-Type: application/json" -X GET https://[DOMAIN].atlassian.net/rest/api/2/serverInfo

Kind regards,
Rafael P. Sperafico
Atlassian Support

Suggest an answer

Log in or Sign up to answer