"Message: Authentification failed" when using REST with JSON

Thursday, March 20, 2014
Avatar
Hello,

When I issue any REST request with Content-Type set to "application/json" in the HTTP header, I always get the following reply from the server:
{"Message":"Authentication failed.","StackTrace":"","ExceptionType":""}

Even with such a message, the request is processed without a problem.

I am testing this in Excel VBA. The following code should reproduce the issue. Please adjust carefully the value for your SpiraTeam configuration.

Public Sub ReproduceJSONContentTypeBug()
    'Issue: When specifying JSON as Content-Type, the server replies
    '{"Message":"Authentication failed.","StackTrace":"","ExceptionType":""}
    Dim BASEURL As String
    Dim PROJECT_ID As Long
    Dim RESSOURCEURL As String
    Dim USERNAME As String
    Dim APIKEY As String
    Dim requestBody As String
    Dim MyRequest As Object
   
    'Please adjust the following variables following your configuration
    BASEURL = "http://XXXXX/SpiraTeam/Services/v4_0/RestService.svc/"
    PROJECT_ID = XX
    RESSOURCEURL = "projects/" & PROJECT_ID & "/incidents"
    USERNAME = "XXXX"
    APIKEY = "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
   
    'Please adjust ProjectId's value accordingly.
    requestBody = "{""ProjectId"":XX,""Description"":""The server replied: 'Authentication failed.' but the incident was created."",""Name"":""Testing""}"

    'HTTP request
    Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    MyRequest.Open "POST", BASEURL & RESSOURCEURL
    MyRequest.setRequestHeader "username", USERNAME
    MyRequest.setRequestHeader "api-key", APIKEY
    MyRequest.setRequestHeader "Content-Type", "application/json" 'THIS LINE TRIGGERS THE ERRONEOUS SERVER'S REPLY.
    MyRequest.setRequestHeader "Accept", "application/json"
    MyRequest.send (requestBody)
   
    'The reply is always "{"Message":"Authentication failed.","StackTrace":"","ExceptionType":""}"
    'But the command processed correctly.
    Debug.Print MyRequest.responseText
    MsgBox MyRequest.responseText
End Sub
2 Replies
Friday, March 21, 2014
Avatar
inflectra.jimx
re: maco Thursday, March 20, 2014
Hi Maxim
if you leave the Content-Type alone and just send the Accept: application/json header, does it then work?

Regards

Jim
Friday, March 21, 2014
Avatar
re: inflectra.jimx Friday, March 21, 2014
Hi Jim, thank you for your answer.

Removing the Content-Type header didn't work because now the server complained about the message being raw data.

However, the error gave me a hint by telling me that the accepted Content-Type are "Json" or "Xml" (notice the capitalized J).
So I retried with
MyRequest.setRequestHeader "Content-Type", "application/Json"
and it worked without the authentication issue.

Seems like a bug that could be fixed by Inflectra. The error message should tell me exactly what is wrong, not some (inexistant) authentication problem.

This solves my question, thanks again!

Spira Helps You Deliver Quality Software, Faster and With Lower Risk

And if you have any questions, please email or call us at +1 (202) 558-6885

 

Statistics
  • Started: Thursday, March 20, 2014
  • Last Reply: Friday, March 21, 2014
  • Replies: 2
  • Views: 18781