Not another 415 unsupported media type?

I’m trying to do a simple post a comment to a story and I can’t get the right response. I’m using php/unirest as in the docs and it looks like I have the correct authentication and headers. I’m not sure what is intended with the Bearer header but I used the token again there too; didn’t appear to change anything.

The only response I see is 415 unsupported media type.

require_once(‘vendor/autoload.php’);
require_once(‘vendor/mashape/unirest-php/src/Unirest.php’);

$token = ‘hellodolly@bingo.com:sometokenhuh’;
$tokenEncoded = base64_encode($token);

//var_dump($tokenEncoded);

$headers = array( ‘Accept’ => ‘Application/json’, ‘Content-Type: Application/json’, ‘Authorization’ => 'Basic '.$tokenEncoded, 'Bearer' => 'abcdefghijklmnopqrstuvwxyz');

// basic auth
Unirest\Request::auth(‘hellodolly@bingo.com’,‘sometokenhuh’);

$body = <<<REQUESTBODY
{
  "visibility": {
    "type": "role",
    "value": "jira-software-users"
  },
  "body": {
    "type": "doc",
    "version": 1,
    "content": [
      {
        "type": "paragraph",
        "content": [
          {
            "text": "Bacon ipsum dolor amet deserunt ham hock picanha magna ground round swine. Bresaola andouille lorem in sunt cupidatat velit boudin in tenderloin kevin kielbasa ham minim ea. ",
            "type": "text"
          }
        ]
      }
    ]
  }
}
REQUESTBODY;




$response = Unirest\Request::post(‘https://wubbalubbadubdub.atlassian.net/rest/api/3/issue/BBB-835/comment’, $headers, $body);

var_dump($response);

https://developer.atlassian.com/cloud/jira/platform/rest/v3/?utm_source=%2Fcloud%2Fjira%2Fplatform%2Frest%2F&utm_medium=302#api-rest-api-3-issue-issueIdOrKey-comment-post

These headers look a bit weird. If you look at the example provided in the API docs:

$headers = array(
  'Accept' => 'application/json',
  'Content-Type' => 'application/json',
  'Bearer' => '<access_token>'
);

You will notice that the ‘Content-Type’ differs from your implementation. The Bearer header is not applicable, since you have chosen for Basic Auth. So you can remove that header, and replace it with the ‘Authorization’ => 'Basic '.$tokenEncoded header.

Now, given that you have already provided the Authorization header, you do not also have to add

// basic auth
Unirest\Request::auth(‘hellodolly@bingo.com’,‘sometokenhuh’);

However, because you are receiving a 415 method not allowed and not a 401, it seems like the authentication is not the issue. So my best bet would be the fact that the Content-Type and Accept headers are malformed.

Can you try with this:

require_once('vendor/autoload.php');
require_once('vendor/mashape/unirest-php/src/Unirest.php');

$token = 'hellodolly@bingo.com:sometokenhuh';
$tokenEncoded = base64_encode($token);

//var_dump($tokenEncoded);

$headers = array(
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'Authorization' => 'Basic '.$tokenEncoded
);

$body = <<<REQUESTBODY
{
  "visibility": {
    "type": "role",
    "value": "jira-software-users"
  },
  "body": {
    "type": "doc",
    "version": 1,
    "content": [
      {
        "type": "paragraph",
        "content": [
          {
          "text": "Bacon ipsum dolor amet deserunt ham hock picanha magna ground round swine. Bresaola andouille lorem in sunt cupidatat velit boudin in tenderloin kevin kielbasa ham minim ea. ",
          "type": "text"
          }
        ]
      }
    ]
  }
}
REQUESTBODY;

$response = Unirest\Request::post('https://wubbalubbadubdub.atlassian.net/rest/api/3/issue/BBB-835/comment', $headers, $body);

var_dump($response);
2 Likes

Thank you so much. I haven’t done anything development related in nearly five years and it has been an uphill crawl to get functional again. This was a big help to my sanity.