swathi
swathi

Reputation: 139

REST API service when called from azure APIM returning empty response body with Status code 200

I have added my REST api service in Azure API Management. I have followed all the steps given in this link Azure APIM. API works fine in local. It also works when accessed through published URL.

I have added OAuth2.0 security as well which is passing through fine.

My issue is When i try to access the operation through APIM published URL, the service is giving response 200 with empty content.

Can Someone please help.

Upvotes: 9

Views: 6652

Answers (5)

Rikin Patel
Rikin Patel

Reputation: 9393

In my case get request working fine but post request retrun 200 but empty response.

so if you have same case then please add cors policy in API management

<policies>
    <inbound>
        <!-- base: Begin Global scope -->
        <cors allow-credentials="true">
            <allowed-origins>
                <origin>http://localhost:4200/</origin>
                <origin>https://developer.mydomain.com</origin>
            </allowed-origins>
            <allowed-methods preflight-result-max-age="300">
                <method>*</method>
            </allowed-methods>
            <allowed-headers>
                <header>*</header>
            </allowed-headers>
            <expose-headers>
                <header>*</header>
            </expose-headers>
        </cors>
        <!-- base: End Global scope -->
    </inbound>
</policies>

More info:

Upvotes: 0

Shylesh R
Shylesh R

Reputation: 1

If you add this:

<backend>
    <forward-request />
</backend>

It will resolve the issue. This part will forward the request to the URL you set as backend service URL.

Upvotes: 0

Hiran
Hiran

Reputation: 1180

As mentioned, you have deleted some policy rules from all APIs. to fix this,

  1. navigate to the API tab in the Azure portal
  2. click on All APIs
  3. click on edit inbound policies
  4. add forward-request policy inside the backend enter image description here

Upvotes: 0

brendanrichards
brendanrichards

Reputation: 329

Old question, but I'm posting because I had a similar issue (empty response) caused by a different issue:

I was getting empty responses as I had expressions in a policy that read the response. By default, reading the response clears it (presumably because its a stream underneath).

Found some good docs here: https://learn.microsoft.com/en-us/azure/api-management/api-management-transformation-policies#SetBody

I fixed this by specifying "preserve content" when reading the response:

JObject responseJobj = null;
if (context.Response.Body != null) 
{ 
    responseJobj = context.Response.Body.As<JObject>(preserveContent: true); 
}

Upvotes: 5

Darrel Miller
Darrel Miller

Reputation: 142222

This problem occurs when people unintentionally delete the top level policy and remove the main forward-request policy that applies to all operations on all apis. Simply click the Add policy button to re-add the default policy at the top level scope.

Upvotes: 15

Related Questions