cybercog / youtrack-php-sdk

YouTrack PHP Software Development Kit provides set of tools to interact with JetBrains YouTrack.

Home Page:https://komarev.com/sources/php-youtrack-sdk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

POST Request to Create Issue Returns 415

dougblackjr opened this issue · comments

Hi there! I am working on implementing this package in our app. I am able to make GET requests successfully; however, when making POST requests, I am consistently getting HTTP 415 Unsupported Media Type returned. I initially tried implementing the POST request example from the documentation, but got a 404. I have tried this with a number of Guzzle params, including setting Accept-Content and Content-Type, but continually get this error.

The code:

$params = [
	'project'		=> [
		'key'	=> $this->settings['project'],
	],
	'summary'		=> $item->title,
	'description'	=> $item->body,
	"issuetype" => [
		"name" => "Bug"
	],
];

$options = [
	'debug'				=> true,
];

$psrHttpClient = new Guzzle([
	'base_uri' => $this->settings['host'],
]);

// Instantiate YouTrack API HTTP Client Adapter
$httpClient = new YouTrackClient($psrHttpClient);

// Instantiate YouTrack API Token Authorizer
$authorizer = new YouTrackAuthorizer($this->settings['token']);

try {

	$this->connection = new YoutrackConnection($httpClient, $authorizer, 'youtrack/api');

	$response = $this->connection->post(
		'/issues',
		$params,
		$options
	);

	$output = $response->toArray();

	$this->output = $output;

} catch (\Exception $e) {

	$this->error = [
		'code' => $e->getCode(),
		'reason' => $e->getMessage(),
		'error' => $e->getPrevious(),
		// 'body' => $e->getResponseBody(),
	];

	return $this->error();

}

The response:

'code' => 415,
'reason' => '{"error":"Unsupported Media Type","error_description":"HTTP 415 Unsupported Media Type"}',

I am able to make the same call via Postman and it is successful.

Hi @dougblackjr! Seems like a bug. You could try to pass 'sink' => '/path/to/store/request-debug.log' to Guzzle request options and check if compiled request is correct.

http://docs.guzzlephp.org/en/stable/request-options.html#sink

@antonkomarev Thank you. Unfortunately, the sink param creates a file but doesn't populate it. I gave it proper file permissions so its openly writable, but nothing coming through.

@dougblackjr are you using Self-Hosted YouTrack or Cloud?

Cloud.

I'm trying to configure it right now and will test it myself.

It looks like YouTrack only accepts json body data requests.
Try to send your request as json instead of form data. It works for me as a workaround for the commands endpoint.

form data:

$response = $this->client->post('/commands', [
    'query' => $query,
    'issues' => $issues,
]);

json:

$response = $this->client->post('/commands', [], [
    'json' => [
        'query' => $query,
        'issues' => $issues,
    ]
]);

@tritumRz is right. JetBrains changed an API in latest versions and this package documentation a bit outdated. Here is how it works now:

$psrHttpClient = new \GuzzleHttp\Client([
    'base_uri' => 'https://example.myjetbrains.com/youtrack/',
]);

$httpClient = new \Cog\YouTrack\Rest\HttpClient\GuzzleHttpClient($psrHttpClient);

$authorizer = new \Cog\YouTrack\Rest\Authorizer\TokenAuthorizer($token);

$youtrack = new \Cog\YouTrack\Rest\Client\YouTrackClient($httpClient, $authorizer, 'api');

$params = [];

$options = [
    'debug' => true,
    'json' => [
        'project' => [
            'id' => '0-0',
        ],
        'summary' => 'Test Summary',
        'description' => 'Test Description',
    ],
];

$response = $youtrack->request('POST', '/issues', $params, $options);

Thank you both. @tritumRz, good call on the JSON, didn't realize it had to get sent with the params.

@antonkomarev, I attempted the example you posted above and am getting a non-API response:

<!doctype html>
<!--[if lte IE 9]>
<html class="no-js">
<![endif]-->
<!--[if gt IE 9]><!-->
<html class="no-js" ng-app="YouTrackLogin" ng-strict-di>
<!--<![endif]-->

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="robots" content="noindex">

  
  <base href="/youtrack/">
  
</head>

<body>
<div yt-loader="screen" message="Loading YouTrack..."></div>

<script src="static/vendor.14602ab87fe1a5c13f2e.js"></script>
<script src="static/oauth.cadc22669acfcdd064a7.js"></script>
</body>

</html>

That's strange this code is working for me on cloud application. I've received such issues when provided base_uri without trailing slash. But after I added it, it started to work as expected.

Not working:
https://example.myjetbrains.com/youtrack

Working:
https://example.myjetbrains.com/youtrack/

@antonkomarev That seems to be the issue. Thank you for your help.

Just to clarify: This is the proper documentation for the newest version? https://www.jetbrains.com/help/youtrack/incloud/resource-api-issues.html

I found a few and just want to make sure I'm in the right place.

Yes you should use YouTrack InCloud 2020.2 documentation, it's current.