How to remove header.Content-type?

vicentelee
Participant II

So my proxy endpoint asks for a json payload. And the user will call the proxy by supplying a json payload and of course Content-Type to application/json.

However the target endpoint asks for a application/x-www-form-urlencoded. I've tried <Remove> and <Set> but not matter what I do, I can't seem to get rid of header.Content-Type.

I've tried removing this header.Content-Type in AssignMessage as well as a ServiceCallout.

0 3 3,524
3 REPLIES 3

You said "I've tried removing this header" but you didn't show the policy configuration you used to try removing it. Or maybe you used some other technique to try to remove it. Care to share?

AssignMessage works for me. I do this all the time to remove arbitrary headers .

<AssignMessage name='AM-RemoveHeader'>
  <DisplayName>##</DisplayName>
  <Remove>
    <Headers>
      <Header name='Content-Type'/>
    </Headers>
  </Remove>
  <AssignTo createNew='false'>message</AssignTo>
</AssignMessage>

But while this WORKS, and it directly answers the question you asked, I think maybe you did not ask the right question. I think simply removing a header is not really what you want to do. If your target is expecting a content-type of application/x-www-form-urlencoded , it's not enough to simply remove the pre-existing content-type header. You need to SET one.

So probably you want something like this:

<AssignMessage name='AM-MyCustomPayload'>
  <AssignTo createNew='false'>message</AssignTo>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <Set>
    <Payload contentType='application/x-www-form-urlencoded'>foo=bar</Payload>      <Verb>POST</Verb>
  </Set>
</AssignMessage>

The contentType attribute on the Payload element there, will implicitly set the Content-Type header.

BTW, the Payload element itself is a Message Template, so you can set it to a dynamically-constructed value by referring to context variables with the curly-brace notation. Like this:

 <Set>
    <Payload contentType='application/x-www-form-urlencoded'>foo={variable_name}</Payload>
     <Verb>POST</Verb>
  </Set> 

If it's just changing the value of the Content-Type header, try a Javascript policy to set the variable to a new value, use context.setVariable("request.header.Content-Type") to set the new value and work from there.

Edit: Or what the above user said, include your policies XML to better understand what you're trying to achieve or what you've tried so far.

@Vincente Lee

If the target endpoint asks for "application/x-www-form-urlencoded", why not set the header Content-Type to "application/x-www-form-urlencoded" in Target PreFlow using AssignMessage Set ?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="set-header-contenttype">
  <DisplayName>set-header-contenttype</DisplayName>
  <Set source="request">
    <Headers>
      <Header name="Content-Type">application/x-www-form-urlencoded</Header>
    </Headers>
  </Set>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

Did a test proxy and it looks fine for me.

curl -X POST http://brendanvu-eval-test.apigee.net/v1/remove-header-contenttype -H 'Content-Type: application/json' -H 'Content-Length: 0'

to see you are sending in application/json, but the header passed to target is "Content-Type": "application/x-www-form-urlencoded"