Can't update jira issue with REST API - rest

I'm trying to do this with powershell, but I'm getting 400 errors:
$RESTURL = 'https://mycomp.atlassian.net/rest/api/latest/issue/PROJ-61'
$body = '{"fields":{"assignee":{"name":"me"}}}'
$restcreds = [System.Convert]::ToBase64String(
[System.Text.Encoding]::ASCII.GetBytes(('me' + ":" + 'mypass123'))
)
$httpheader = #{Authorization = "Basic $restcreds"}
$restParameters = #{
Uri = $RESTURL;
ContentType = "application/json";
Method = "PUT";
Headers = $httpheader;
Body = $body;
}
Invoke-RestMethod #restParameters
If I remove "body" from the request and change it to a get I get back data successfully. It seems I just get modify the ticket

If you get a 400 (bad request), then that means something is wrong in your request body.
The response body will contain a more detailed error message and will make it clear what you have to fix.
Without the error message, I can only make a guess:
I'm not sure if setting assignee to "me" works, unless "me" really is the name of a user. What happens if you try with a complete username or if you use "key" instead of "name"?
The fact that a GET request works fine shows that your credentials are correct, so it's not an authentication problem.

Related

Purview API Create glossary terms

I am trying to reference the following page
https://learn.microsoft.com/en-us/rest/api/purview/catalogdataplane/glossary/create-glossary-term?tabs=HTTP
$glossarytermcreateendpoint = 'https://pv-chn-asiabi-sea-dev.purview.azure.com/catalog/api/atlas/v2/glossary'
$Glossaryterm = #{
qualifiedName = "sample"
name= "sample"
longDescription = "sample"
shortDescription = "sample"
status = "Draft"
}
$jsonbody = $Glossaryterm | ConvertTo-Json
$Responsepost = Invoke-WebRequest -Uri $glossarytermcreateendpoint -Method Post -headers $headers -Body $jsonbody -ContentType 'application/json'
I am ok to get status 200
however in the Purview workspace , I can't find the new term is created
Questions
How to make it works? Able to search ?
How to create a glossary term under specific term template .?
$glossarytermcreateendpoint = 'https://pv-chn-asiabi-sea-dev.purview.azure.com/catalog/api/atlas/v2/glossary'
From your script, I noticed that the API you called is to create a glossary, not a term, so you can't find the new term you created.
The right API to create a term is to append /term to the end of your current API:
$glossarytermcreateendpoint = 'https://pv-chn-asiabi-sea-dev.purview.azure.com/catalog/api/atlas/v2/glossary/term'
Let me know if this solves your problem.

Powershell - Invoke-RestMethod - POST nested JSON

I'm trying to interact with an API to POST some data to a service we use, in order to add some email addresses to a policy. This will eventually take a list of email addresses and loop through it for each one, but for now I'm just trying to get it to work with a single address.
I'm using Powershell 7, I would say I'm intermediate with powershell, but this is my first foray into interacting with an API, using JSON, and using the Invoke-RESTMethod commandlet. The API reference for the service shows lots of code examples in other languages, just not PS!
Here's the problem I'm running in to. I'm trying to formulate a -Body statement that looks to be 3 elements instead of two. Here is what that example statement looks like in CURL:
{
"custodians": [
{
"emailId": "john.doe#someorg.com"
}
],
"action": "add"
}
The Action:Add part is fine, easy peasy. I'm trying to figure out how to correctly format the custodians part. I cant figure out how to do 3 elements instead of two. I've tried different combinations of {} [] and () to no avail:
"custodians" = ({"emailId" = "emailaddress#place.com"})
gives me an "The assignment expression is not valid" error
"custodians" = [{"emailId" = "emailaddress#place.com"}]
and
"custodians" = [#{"emailId" = "emailaddress#place.com"}]
give me an "Missing type name after '['." error.
These, as well as a few other combinations I've typed out, all showed formatting errors in VSCode so I already knew they wouldn't work, I'm just not sure why. I'm sure I just haven't cracked the right combination of #, {}, [] or () but I cant seem to find any info online (probably because I'm not using the right vocabulary or phrasing in my searches) that shows how to format with three elements.
If its relevant or helpful, here is a larger code sample of the whole query Im working on. Assume the Auth headers are fine (I can request my auth tokens and do GETs without issue so far):
$headers = #{
"Authorization" = $bearerAuthValue
"Accept" = "application/json"
"Content Type" = "application/json"
}
$body = #{
"custodians" = #(emailId = "emailaddress#place.com"),
"Action" = "add"
}
$uri = "https://thisisanaddress.com"
Invoke-RestMethod -Method 'POST' -Uri $uri -Headers $headers -Body $body
You are looking at having multiple emailids in custodians?
$body = #{
custodians = #(
#{emailId = 'emailaddress#place.com' }
#{emailId = 'emailaddress#place.com' }
#{emailId = 'emailaddress#place.com' }
)
Action = 'add'
}
Output:
$body | ConvertTo-Json
{
"Action": "add",
"custodians": [
{
"emailId": "emailaddress#place.com"
},
{
"emailId": "emailaddress#place.com"
},
{
"emailId": "emailaddress#place.com"
}
]
}

Syntax for groovy to duplicate PowerShell Invoke-RestMethod

I'm trying to duplicate the PowerShell Invoke-RestMethod to something similar in Groovy (groovy is standard we use in our coded pipeline).
I've done a lot of searching without success. I was wondering if I can get some help or suggestions on a possible alternative if there isn't a similar call?
The 3 three lines of PowerShell I'm trying to duplicate in Groovy are:
$tokenrequest = #{ "grant_type" = "password"; "username" = "adminuser"; "password" = "adminpassword" }
$token = Invoke-RestMethod -Uri "http://abcd.com/executionmanager/api/Token" -ContentType application/x-www-form-urlencoded -Headers #{ Authorization = ("OAuth2")} -Method POST -Body $tokenrequest
$token = $token.access_token
there are a lot of libraries to do that
the following one quite unknown
//download dependency from maven repository
#Grab(group='acme.groovy', module='acmehttp', version='20180403')
import groovyx.acme.net.AcmeHTTP
def ctx = AcmeHTTP.post(
url: "https://httpbin.org/post", //the url could be used for tests
//define body as map as soon as we are sending it as www-form
body: [ "grant_type": "password", "username": "adminuser", "password": "adminpassword" ],
headers:[
"content-type":"application/x-www-form-urlencoded",
"Authorization": "OAuth2"
],
)
assert ctx.response.code==200
//the url https://httpbin.org/post always returns json
assert ctx.response.contentType =~ "/json"
assert ctx.response.body instanceof Map
println ctx.response.body
//i guess your token should be accessible like this:
//println ctx.response.body.access_token

Guzzle returning a 404 on a valid URL

I'm using Guzzle with CurlAuthPlugin to authenticate. When I run the code, I get
Client error response\ [status code] 404\ [reason phrase] Not Found\ [url] https:\/\/api.buto.tv\/v2\/video\/tag\/v2\/video\/tag\/
The code I'm using is:
$client = new Client(<URL>);
// Add the auth plugin to the client object
$authPlugin = new CurlAuthPlugin(<APIKEY>, 'x');
$client->addSubscriber($authPlugin);
$response = $client->get('v2/video/tag/')->send();
But the URL is perfectly valid a I can paste that in to a browser and it works fine
I've also tried:
$client = new Client('https://api.buto.tv');
$request = $client->get('v2/video/tag/');
$request->setAuth('user', 'pass');
$response = $request->send();
But I get the same error. I have output the URL it's requesting with echo $request->getUrl(); and if I copy and paste the URL in to a browser, the URL is fine
I think you may be missing a slash '/' after api.buto.tv, so the url is resolving to 'https://api.buto.tvv2/video/tag/' instead of 'https://api.buto.tv/v2/video/tag/'.
$client = new Client('https://api.buto.tv/');
$request = $client->get('v2/video/tag/');

Getting Zend_Http Final URL

Making a simple request like:
$client = new Zend_Http_Client('http://example.org');
$response = $client->request();
How can I get the final URL after the redirects?
I have not seen a way in the documentation or the API docs unless I'm missing something.
Thanks in advance.
Zend_Http_Client update the last URL into Zend_Http_Client->uri property if there is redirect.
$sourceUrl = 'http://google.com';
$client = new Zend_Http_Client($sourceUrl);
$response = $client->request();
$finalUrl = $client->getUri()->__toString();
var_dump($sourceUrl);
// string(17) "http://google.com"
var_dump($finalUrl);
// string(25) "http://www.google.com:80/"
Not tested :
$response->getHeader('Location');
Get the last request from the client and then extract the headers.
$client = new Zend_Http_Client('http://webonyx.com');
$response = $client->request();
$lastHeaders = Zend_Http_Response::extractHeaders($client->getLastRequest());
// $lastHeaders['host'] will be your final redirected host