MoskiMeruna
MoskiMeruna

Reputation: 27

How to POST Content-Type application/x-www-form-urlencoded in Azure API Management body?

I have been trying to figure out a way to POST Content-Type application/x-www-form-urlencoded in Azure API Management body.

I have managed to make it work in Postman because it supports x-www-form-urlencoded but can't seem to find a way to do it in the API Management. No matter where and how I try to POST the body in the API Management it gives an error: There was error parsing the request. Expected format: { token: string, enrollDevice: bool }. In the Postman, I can just put the value into the POST body using x-www-form-urlencoded in format {"token":"xxx","enrollDevice":xxx} and it works!

How could I POST the value needed in the body using Azure API Management?

If you need more info related to the problem I'm happy to give more info. Thanks for the help in advance! :)

Upvotes: 2

Views: 4656

Answers (2)

Linus Proxy
Linus Proxy

Reputation: 593

I've got a similar issue and I just figured out the solution, maybe it can help you or anyone else having a similar issue.

I also had a fully working Postman request with a x-www-form-urlencoded body, in my case to post a client ID and secret to an OAuth token endpoint to get a bearer token back. For every call in my API that I registered in Azure API Management, I wanted a policy to first go fetch a valid bearer token at the token endpoint and then call the API with that token, so I added a custom policy with a to all operations. No matter what I did in my custom policy in Azure API Management, the token endpoint just said I was missing parameters.

The solution was to format the contents of to look like a regular URL query string.

<inbound>
    <base />
    <send-request ignore-error="false" timeout="20" response-variable-name="accessTokenResponse" mode="new">
        <set-url>https://xxxxxxxxxxxxxxxxxxxxxxx/oauth2token</set-url>
        <set-method>POST</set-method>
        <set-header name="Content-Type" exists-action="override">
            <value>application/x-www-form-urlencoded</value>
        </set-header>
        <set-body>@{
            return "scope=dev-scope&client_id=1234567&client_secret=abcdefg";
        }</set-body>
    </send-request>
    <set-variable name="accessTokenResponseBody" value="@(((IResponse)context.Variables["accessTokenResponse"]).Body.As<JObject>())" />
    <set-variable name="bearerToken" value="@((string)((JObject)context.Variables["accessTokenResponseBody"])["access_token"])" />
    <set-header name="Authorization" exists-action="override">
        <value>@("Bearer " + (string)context.Variables["bearerToken"])</value>
    </set-header>
</inbound>

Upvotes: 2

SaiKarri-MT
SaiKarri-MT

Reputation: 1301

We can add a policy to set the headers as name = “Content-Type” :

<set-header name="Content-Type" exists-action="override">
    <value>application/json</value>
</set-header>

Add all the format in inbound policy section, basically we have inbound, outbound, backend, and on-error policies. Check for MS Docs for setting the HTTP Headers.

<inbound>
    <base />
    <set-header name="Content-Type" exists-action="override">
        <value> application/x-www-form-urlencoded</value>
    </set-header>
    <set-body template="liquid">{"QueryString": "123", "param1": "456"}</set-body>
    <set-body>@{ 
        JObject inBody = context.Request.Body.As<JObject>(); 
        return inBody.ToString(); 
    }</set-body>
</inbound>

Also make sure that body has the correct content.

Upvotes: -1

Related Questions