Couldnt resolve REST put request. Does it work?

Dana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 8, 2013

According to https://developer.atlassian.com/static/rest/stash/2.6.3/stash-rest.html#idp2202432

I am trying to send put request /rest/api/1.0/users.

But there is always 405 error.

When I am trying to make request from Stash-Administration-REST API browser-Atlassian Stash REST plugin - /users - put it makes GET request.

Does the error in the rest? or I am wrong?

Simply I just want to retrieve currently authenticated user? Does any (undocumented?) default rest api exist? Or the better way is to make my own rest with the help of StashAuthenticationContext class?

PS Example of my request (via jQuery.ajax)

==1==
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "[local path]/stash/rest/api/1.0/users",
                success: function (data) {
                    alert(data);
               }
            }); 
==2==
var dataInfo =  '{"active": "true" }'; //or {} , or smth else, tried several variants
            $.ajax({
                type: "POST",
                data: dataInfo,
                dataType: "json",
                url: "[local]/stash/rest/api/1.0/users",
                success: function (data) {
                    alert(data);
               }
            });


9 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Dana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 27, 2013

I have decided it by jQuery('#current-user').attr('data-username').

My goal was to retrive current authenticated user comments.

1 vote
Ignat
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 8, 2013

Dear Dana,

In your given code examples you are using POST instead of PUT, that's why you get HTTP 405 status ("Method not allowed").

Try to execute PUT with jquery.

--

Cheers, Ignat.

Dana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 8, 2013

It didnt help. Now 415.

Julian Riedinger _Decadis AG_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 8, 2013

can you print an example of your REST-entry-point

415 says that the media types doesn't match. You've put json on the one hand. What you've expacted on the other?

Dana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 8, 2013

I am expected JSOn with currently authenticated user's details.

I wanna to send empty json ({}), to not to change anything, but get response. Unfortunately, I didnt find more appreciable REST-reqest.

I tried several variants fron javascript-console inside browser

jQuery.ajax({
                type: "PUT",
                dataType: "json", data: ({}),
                url: "[local]/stash/rest/api/1.0/users",
                success: function (data) {
                    alert(data);
                }
            });

Its very strange that REST API browser doesnt work proper, too.


Pawel Skierczynski
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 8, 2013

Did it happen from browser plugin? I'd avoid using it for calling REST endpoints. There is a known issue that it doesn't send correct content type header. https://ecosystem.atlassian.net/browse/DEVBOX-29.

When sending with jQuery try to ensure that header Content-Type is set.

Look here: http://forum.jquery.com/topic/jquery-headers-support-for-ajax

headers: {
        "Content-Type": "application/json",
        "Accept": "application/json"
    },

' dataType: "json" ' may be not enough

Dana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 8, 2013

Thank you. But it still doesnt work.

jQuery.ajax({

beforeSend: function (xhrObj) {
                            xhrObj.setRequestHeade("Content-Type", "application/json");
xhrObj.setRequestHeader("Accept", "application/json");
},
                type: "PUT",
                dataType: "json", data: ({}),
                url: "http://kaetana-osx.local:7990/stash/rest/api/1.0/users",
                processData: false,
                success: function (data) {
                    alert(data);
                }
            });

Does anybody tried to repeat this request?

0 votes
Ignat
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 8, 2013

Hi Dana,

Hope this will help:

jQuery.ajax({
    type: "PUT",
    data: JSON.stringify({
    "name" : "<name>",
    "displayName" : "<displayName>",
    "email" : "email@example.com"
}),
contentType:"application/json; charset=utf-8",
    url: "/rest/api/1.0/users",
    success: function (data) {
        log(data)
    }
});

--

Cheers, Ignat.

0 votes
Adam
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 8, 2013

If you're trying to call this from Stash itself, that information should be mostly available on the page without a REST request at all.

This code isn't a public API, so it may stop working in a future release.

var user = require('model/page-state').getCurrentUser().toJSON();

console.log(user.name, user.displayName, user.slug);

What do you plan to do with the current user information?

Cheers,

Adam

0 votes
Pawel Skierczynski
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 8, 2013

I've tried and I'm afraid it will not accept empty update.

In JIRA there is a new endpoint called myself, but I haven't found anything like that on stash...

Maybe as a workaround you could use something like that:

Call anything and get X-AUSERNAME from header response.

Then try to use it with GET /stash/rest/api/1.0/users/{userName}.

I'm not 100% sure if that's the same field that in header and it's ugly but it may be worth a try.

0 votes
Julian Riedinger _Decadis AG_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 8, 2013

Change your AJAX-Request type to put

0 votes
Dana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 8, 2013

I dont understand clear. I have tried to send request via js-script, and via REST API borwser. Both ones are awry.

0 votes
Julian Riedinger _Decadis AG_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 8, 2013

can you copy the header of your method?

0 votes
Ignat
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 8, 2013

Dear Dana,

Are you able to execute PUT request from external REST client (e.g. a small python script)? I assume you may experience some issue with REST API browser.

--

Cheers, Ignat.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events