1

I get the following error when I try to do a post to my Azure REST API app:

Exception: Exception caught: 'Microsoft.Rest.HttpOperationException' in Program.exe ("Operation returned an invalid status code 'Created'"). Exception caught: 'Microsoft.Rest.HttpOperationException' in Program.exe ("Operation returned an invalid status code 'Created'") 13,495.67s [7568] Worker Thread

It posts correctly to the API app (and places the information in the DB the API app talks to), but for some reason it looks like the return code isn't handled correctly. This post functionality was auto generated for me when I added the REST API Client to my solution. For a work around I just nested it in a try/catch, but I really want to solve this so exceptions that are flagged when debugging are legitimate.

2
  • Azure also allows us to remote debug with Vistual Studio. Accoring to execpiton I assume that maybe there is something wrong with logic in the Rest API. Please have a try to remote debug it. If it is also not solved, please share your API code if it is possible. We also can use fiddler to catch the http request for more details. Commented Mar 7, 2017 at 6:10
  • @TomSun-MSFT It is returning a status code that the Rest.HttpOperation isn't familiar with. Is this status code defined somewhere? How can I use remote debugging with the API app if everything is working as expected? It is returning the 201 code and successfully creating in the DB using the API app, but that code just isn't getting handled by Microsoft.Rest.HttpOperation. Commented Mar 20, 2017 at 18:14

1 Answer 1

3

According to the Microsoft.Rest.ClientRuntime.Azure source code CheckResponseStatusCodeFailed function, we may know that if the httpStatusCode.Created && method == HttpMethod.Put then CheckResponseStatusCodeFaild function will return false. But in your condtion that httpStatusCode.Created && method == HttpMethod.Post, then CheckResponseStatusCodeFailed equals true. Then it will throw the exception.

private static bool CheckResponseStatusCodeFailed<TBody, THeader>(
            AzureOperationResponse<TBody, THeader> initialResponse)
        {
            var statusCode = initialResponse.Response.StatusCode;
            var method = initialResponse.Request.Method;
            if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Accepted ||
                (statusCode == HttpStatusCode.Created && method == HttpMethod.Put) ||
                (statusCode == HttpStatusCode.NoContent && (method == HttpMethod.Delete || method == HttpMethod.Post)))
            {
                return false;
            }
            return true;
    }

In my option, please have a try to change HttpMethod.Post to HttpMethod.Put or add the SwaggerResponse attributes [SwaggerResponse(HttpStatusCode.OK)] to the Web API action methods for which you want to specify valid HTTP response codes. More info about SwaggerResponse attributes, please refer to customize expected response codes using the SwaggerResponse attribute.

Sign up to request clarification or add additional context in comments.

4 Comments

Swagger response attribute did it! Thanks Tom!
None of the solutions worked for me. Do you know why POST are not allowed to send status code 201 "Created"? "The common use case of this status code is as the result of a POST request." developer.mozilla.org/en-US/docs/Web/HTTP/Status/201
@Peheje. The common use case of this status code is as the result of a POST request I agree with you. But I am not the owner of the SDK, I just get the source code from the github.
Hi Tom. It was not an accusation, more a gereral question for you or others. Glad we agree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.