question

DanielAlvesLeandro-5849 avatar image
0 Votes"
DanielAlvesLeandro-5849 asked MayankBargali-MSFT edited

Usign set-body to format payload for Kafka REST

Hi,

I have an API on Apigee that sets the content type of the request that is sent in JSON format to vnd.kafka.json.v2+json and then it converts the payload of the request to the desired format before sending it to Kafka (https://docs.confluent.io/platform/current/kafka-rest/api.html#crest_v2_api). In Apigee it seems to be simple, you have just to do that:

 <Set>
     <Headers>
         <Header name="Content-Type">
             application/vnd.kafka.json.v2+json
         </Header>
         <Header name="Authorization">Bearer {output_jwt}</Header>
     </Headers>
     <Payload name="request.content">{
         "records": [
             {
                 "value": {request.content}
             }
         ]
     }
     </Payload>
 </Set>

I need to migrate the same API to API Management on Azure, but I couldn't get it working yet. I'm trying to do the same using set-body, but I got error 500 (Internal Server Error), because the JSON generated is not valid. I've tried in several ways like these below but it didn't work.

1)



 <set-header name="Content-type" exists-action="override"><value>application/vnd.kafka.json.v2+json</value></set-header>
 <set-body>{ "records": [{ "value": {context.Request}}]}</set-body>

2)

  <set-header name="Content-type" exists-action="override"><value>application/vnd.kafka.json.v2+json</value></set-header>
 <set-variable name="payload" value="@(context.Request.Body.As<JObject>())" />
 <set-body>{ "records": [{ "value": context.Variables["payload"]}]}</set-body>

Could you help me with this please? It can be using approach above, if possible, or any other that works.

Thanks in advance.

Best Regards

azure-api-management
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MayankBargali-MSFT avatar image
0 Votes"
MayankBargali-MSFT answered MayankBargali-MSFT edited

@DanielAlvesLeandro-5849 Assuming that you want to set whatever value is there in the body while calling the APIM operation you can use the set-body policy to set the body. You can modify it as per your need.

For example I am assuming the string value as "This is test" I am converting the context.Request.Body.As<string>() but in case it is json object you can use context.Request.Body.As<JObject>()

128284-image.png

Similarly to set the header you can use the set-header. You can refer to this document for Deep Dive on set-body Policy.


image.png (7.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.