JIRA Worklog API not working - jira

I am trying to log work in JIRA using the Web API :-
My data is:
var post = {};
post.commment = "Test";
post.timeSpent = "6h";
My Ajax call is:
$.ajax({
url: lv_url,
type : 'POST',
data : post,
headers: {
Accept : "application/json; charset=utf-8",
},
contentType: "application/json; charset=utf-8",
dataType : 'json',
xhrFields: {
withCredentials: true
},
async: false,
success : function(data) {
}
});
https://jiraserver.co/rest/api/2/issue/SOCOMPT-1575/worklog
"GET" call is working fine but when i try to POST i get the error:-
1) OPTIONS https://jiraserver.co/rest/api/2/issue/SOCOMPT-1575/worklog 500 (Internal Server Error)
2) XMLHttpRequest cannot load https://jiraserver.co/rest/api/2/issue/SOCOMPT-1575/worklog. Invalid HTTP status code 500
These are the 2 errors is get.
Please Help Guys i really need to get this working.
Thanks in advance,
Vishesh.

I was also strugging on this one as I kept getting HTTP 500 when trying to post to the worklog endpoint.
if you are able check the jira server logs (under logs/catalina.out)
jira seems to be very picky with the iso8601 date format
Try setting also the "started" timestamp in your payload as I believe this is required (for the API like the web UI) even if the documentation is not really clear on that.
post.started = '2015-02-25T14:01:30.000-0500';

Related

Ajax calls to TFS 15 Rest Api stopped working after upgrade

In TFS 2015 Update 3 everything was working without issues. I used to consume all apis using the npm package request without any problems.
Using jquery the following call also would always complete correctly:
//this used to work in 2015 Update 3
var request = {
url: "https://my_server/tfs/DefaultCollection/_apis/projects?api-version=2.0",
type:'GET',
contentType: "application/json",
accepts: "application/json",
dataType: 'json',
data: JSON.stringify(data),
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("my_username:my_password"));
}
};
$.ajax(request);
After upgrading to TFS 15 RC2 the above mechanism is not working anymore. The server always returns a 401 - Unauthorized error.
Testing the same call via curl, everything worked out well:
//this works well
curl -u my_username:my_password https://my_server/tfs/DefaultCollectiopis/projects?api-version=2.0
But again failed when I tried to send the credentials in the header, something like this:
//this will fail
curl https://my_server/tfs/DefaultCollection/_apis/projects?api-version=2.0 \
-H "Accept: application/json" \
-H "Authorization: Basic eNjPllEmF1emEuYmFuNppOUlOnVuZGVmaW5lZA=="
Same 401 - Unauthorized error.
I tried to set up my Personal Access token, since it is included in TFS 15 RC2, and do a test as indicated here
$( document ).ready(function() {
$.ajax({
url: 'https://my_server/defaultcollection/_apis/projects?api-version=2.0',
dataType: 'json',
headers: {
'Authorization': 'Basic ' + btoa("" + ":" + myPatToken)
}
}).done(function( results ) {
console.log( results.value[0].id + " " + results.value[0].name );
});
});
and it also fails. However, after replacing myPatToken for my actual password and passing my username as well, then the request completed successfully:
//everything works correctly with the headers like this
headers: {
'Authorization': 'Basic ' + btoa("my_username:my_password")
}
In the nutshell, something is going wrong when I setup the header like this (using jquery):
//this fails
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("my_username:my_password"));
}
And looks like the package npm request, which is the one I'm using, also probably uses the beforeSend property or similar and it's failing.
//this used to work with 2015 Update 3, not anymore after upgrading to 15 RC2
var options = {
url: 'https://my_server/defaultcollection/_apis/projects?api-version=2.0',
method:'GET',
headers: {
'Authorization': "Basic " + btoa("my_username:my_password")
},
json: data
};
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
console.log(response);
} else {
console.log(error);
}
});
It makes me think it is probably something in the IIS configuration but Basic Authentication is properly configured. Is there a way to get this working using the package request?
Something changed in the IIS configuration after the upgrade?
The problems got solved after restarting the server, so I guess my case was an isolated situation, not related with TFS it self.
Now sending the request using the request package seems to work well.
However, it is strange that testing it in the browser using jquery still fails. I noticed that
var username = "my_username",
password = "my_password";
btoa(my_username + ":" + my_password); //generates wrong encoded string
generates a different encoded string then simply
btoa("my_username:my_password") //generates right encoded string
The right one is a few characters shorter, this is an example:
eXVuaWQuYmZGV12lpmaW5lZA== //wrong
eXVuaWQuYmZGVsbEAxMw== //correct
No idea why, though.

Why am I suddenly getting an error about no Access-Control-Allow-Origin header?

Yesterday, I was happily making API calls to SurveyMonkey from my local development machine (http://localhost:nnnn)
This morning however (after changing exactly nothing), I'm suddenly getting the following error:
XMLHttpRequest cannot load https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=xxxxxxxxxx. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8500' is therefore not allowed access. The response had HTTP status code 596.
The code I'm using which generates this error is the following:
$.ajax({
url: 'https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=xxxxxxxxxx',
type: 'post',
contentType: 'application/json',
cache: false,
dataType: 'json',
data: JSON.stringify({
survey_id: 'nnnnnnnn',
respondent_ids: respondents //respondents is an array defined elsewhere
}),
headers:{
Authorization: 'bearer ' + local.SurveyMonkey.AccessToken,
'Content-Type': 'application/json'
},
success: function(survey, textStatus, jqXHR){
$('#SMResults').empty().append('<br />'+textStatus);
$p.fnLog(survey.data);
$p.fnLoadView('surveyView.cfm','SMResults',survey);
},
error: function(jqXHR, textStatus){
$('#SMResults').empty().append('Oh nos!! Something went wrong!');
$p.fnLog(jqXHR);
$p.fnLog('----------------------');
$p.fnLog(textStatus);
},
complete: function(jqXHR, textStatus){
$('#SMResults').append("<br /><br />We're done.");
}
});
Again, this code was working brilliantly last night. My attempts to turn this into a JSONP call failed. :(
Any idea what's going on? Why could I happily make these calls yesterday, but not today?
Thanks for reading.
UPDATE: It turns out upon further inspection of the response headers I'm getting:
X-Mashery-Error-Code: ERR_596_SERVICE_NOT_FOUND
Which is why there's not the appropriate Access-Control header in the response. Any idea what's up with Mashery? or Where I should be looking?
UPDATE 2: I'm feeling much more confident that this is a SurveyMonkey/Mashery problem. I just now visited the SurveyMonkey API Console page and it informed me that the console is experiencing a problem and that they're working to fix it.
I'm guessing that this is just a symptom of a larger problem.

Execute controller action with Dojo sending the AntiForeignKey in MVC5

How can I execute a controller action in ASP.NET MVC4 sending the anti foreign key too?
My request is formed as follow code snippet:
var _antiForeignKey = dojoQuery('input[name="__RequestVerificationToken"]', dojo.byId('#__AjaxAntiForgeryForm'))[0].value;
xhr.post({
url: 'Account/LogOff',
handleAs: 'json',
contentType: 'application/json',
postData: '__RequestVerificationToken=' + _antiForeignKey
});
And I receive an error from server with a html which contains the
The required anti-forgery form field "__RequestVerificationToken" is
not present.
message as response. Obviously, the action in the controller is not executed.
I've seen this post: jQuery Ajax calls and the Html.AntiForgeryToken(), which answers my question but using jQuery.
I have got to execute the controller action by using the previous code snippet but modifying the value of two parameters, 'handleAs' and 'contentType':
var _antiForeignKey = dojoQuery('input[name="__RequestVerificationToken"]', dojo.byId('#__AjaxAntiForgeryForm'))[0].value;
xhr.post({
url: 'Account/LogOff',
handleAs: 'text',
contentType: 'application/x-www-form-urlencoded',
postData: '__RequestVerificationToken=' + _antiForeignKey
});
As you can see, I've changed the 'handleAs' property because we are not handling json data in the response, and the 'contentType' to specify we are sending other type of data as request's parameter.

Encrypt (or hide) an auth token being sent through AJAX request

I'm making an ajax request to an API which requires an auth token to be sent in the HTTP headers. Here's what I've got:
$.ajax({
type: 'GET',
url: 'http://foo.com/bar.json',
headers: { "Authorization": 'Token token=' + SECRET_KEY },
dataType: 'json',
...
}
Thing is I don't want SECRET_KEY to be publicly visible if someone were to view the javascript file. Can't seem to find a good workaround, but can't imagine no one else has encountered this... This request is being sent to a Rails app FWIW.

Encoding error in ajax request to rails application

I am working on an application in which i have a situation to send text data to backend through Ajax call.
But when the text "%" occurs in the text to be sent through ajax, i get the below response from my rails app
Internal Server Error
invalid %-encoding
i tried adding escape characters infrot of % symbol, but its not useful.
Any suggestion on this issue would be helpful.
Jquery ajax call used :
thanks,
Balan
You can pass jquery an object for the data option and it should correctly escape it for you:
$.ajax({
type: "POST",
url: "/controller",
data: {
text: text_from_text_area,
current_poster: current_poster
},
success: function(data){
alert(data);
}
});
See the docs here: http://api.jquery.com/jQuery.ajax/

Resources