Jira API 400 Bad Request when creating a ticket on Jira Prod Instance but not on Jira Dev Instance

Oliver Hart September 18, 2018

Hello,

I have a php Project, this creates Jira Tickets over the API.
The authentification works with publickey and the atlassian OAuthWrapper class (https://bitbucket.org/atlassian_tutorial/atlassian-oauth-examples/src/default/php/src/Atlassian/OAuthWrapper.php).

The ticket creation works perfect on a Jira Development Instance but not on a productive cloud instance. On the productive instance, I get a 400 Bad Request answer.

The Source Code for the Request (shorted):

require_once __DIR__ . "libs/jira/vendor/autoload.php";
$oauth->setPrivateKey($pathToPrivateKey);
$oauth->setConsumerKey($jiraConsumerKey);
$oauth->setConsumerSecret($jiraConsumerSecret);
$oauth->setCallbackUrl(urlencode($appServerName . $appPath . "connect/callback/"));
$oauth->setRequestTokenUrl("plugins/servlet/oauth/request-token");
$oauth->setAuthorizationUrl("plugins/servlet/oauth/authorize?oauth_token=%s");
$oauth->setAccessTokenUrl("plugins/servlet/oauth/access-token");
$obj_request = $oauth->getClient($oauth_token, $auth_token_secret)
    ->post("rest/servicedeskapi/request", [
        "Content-Type" => "application/json"
    ], json_encode([
        serviceDeskId" => 16,
        "requestTypeId" => 500,
        "requestFieldValues" => [
            "summary" => "SomeText",
            "description" => "Ticket Discription"
        ]
    ]))->send()->json();

 

As written above, the 400 Bad Request happens only in productive not in the dev instance.

I hope somebody can help me with this problem.

 

Best regards

Oliver Hart

3 answers

1 accepted

1 vote
Answer accepted
Oliver Hart September 20, 2018

I have found the solution of my problem.

 

I have send

summary

description

but the Request Type has only the summary field and not the description field.

I have finally found the missing JSON Response. With Postman not with PowerShell.

I have saved the output and if you save the response it doesn't show up in the program.

 

I have to thanks @Alexey Matveev and @Payne for your ideas.

0 votes
Payne
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.
September 19, 2018

You're specifying project ID 16 and request type ID 500; are they the same in your 2 instances?

Oliver Hart September 19, 2018

No, the Development Instance has only 1 Project, only 3 Custom RequestTypes and some RequestTypes from Jira (ca. 19 Request Types at all)

Payne
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.
September 19, 2018

That may well be your problem; you're trying to create an issue in a non-existent project for a non-existent request type.

Oliver Hart September 19, 2018

Unfortunately not. I get the IDs from the API.

 

API Call for all Servicedesks:

https://x.jira.com/rest/servicedeskapi/servicedesk

 

API Call for the Custom RequestTypes:

https://x.jira.com/rest/servicedeskapi/servicedesk/{servicedeskID}/requesttype

0 votes
Alexey Matveev
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.
September 19, 2018

Hello,

A json must be returned with the 400 error, in which you can see the exact error. Could you have a look what is inside this json?

Oliver Hart September 19, 2018

Hello,

thanks for the quick answer.

I have exportet the Code in an own file and wrapped it in a try and catch statment (If I doesn't make the Try and Catch, I get a 500 Error because of the 400 Error) and made a Test run.

I also var_dump() the Exeption and got this:

string(129) "Client error response
[status code] 400
[reason phrase] Bad Request
[url] https://x.jira.com/rest/servicedeskapi/request"

 I don't know if this helps, but I don't have something else.

 

Best regards

Oliver Hart

Alexey Matveev
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.
September 19, 2018

I am not familiar with php. But there must be a more meaningful string in response.

Oliver Hart September 19, 2018

Hello,

I have checked my Request with Postman and also with PowerShell "Invoke-WebRequest" Here are the specs

PowerShell:

$url = "https://x.jira.com/rest/servicedeskapi/request"

$header =  @{"Authorization" = "Basic -base64string-";"X-Atlassian-Token" = "nocheck"}

$body = @"
    {
        "serviceDeskId":"16",
        "requestTypeId":"500",
        "requestFieldValues":{
            "summary":"Summary Text",
            "description":"Deesscription Text"
        }
    }
"@

$res = Invoke-WebRequest -Uri $url -Method Post -Body $body -Headers $header -ContentType "application/json"
Write-Output $res.StatusCode

PowerShell Output:

Invoke-WebRequest : The remote server returned an error: (400) Bad Request.
In line:16 Character:8
+ $res = Invoke-WebRequest -Uri $url -Method Post -Body $body -Headers  ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequ
   est], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequest
   Command

 

 

Postman:

 

Authentification:

 

 post1_mod.png

 

Headers:

 post2_mod.png

 

Request-Body:

 

 post3_mod.png

 

Response:  (The Body is empty therefore I have the headers screenshotted)

 post4_mod.png

 

--------------------------------------

This is all I get from the API

Best regards

Oliver Hart

Alexey Matveev
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.
September 19, 2018

Try to install Rest Api Browser to your production instance and execute your rest call from there. It is a free add-on.

Oliver Hart September 19, 2018

There is only one Problem, the production instance is a jira-cloud instance and not a server instance. So this won't work.

Suggest an answer

Log in or Sign up to answer