duplicate content-type values causing 415 unsupported mediatype errors

Hi @apickelsimer @kurtkanaskie  We have an client app sending value of content-type twice as below

content-type: application/json,application/json 

Is there a way in apigeex to check this & fix it before sending to backend correctly with the apigeex policies or does it need a javascript?

 

Thanks!!

Raghu

Solved Solved
0 2 393
1 ACCEPTED SOLUTION

Hi Raghu,

This can happen if the client sends duplicate headers, they get concatenated into a single comma separated header value even if specified using curl as:

curl https://hostname/path -H Content-type:application/json -H Content-type:application/json

You can use an Assign Message to "clean" multiple values:

<AssignMessage continueOnError="false" enabled="true" name="AM-clean-content-type">
    <AssignVariable>
        <Name>regex</Name>
        <Value>application/json,</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>request.header.content-type</Name>
        <Template>{replaceAll(request.header.content-type,regex,'')}</Template>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

The assignment of the string to the "regex" is required as the literal value doesn't work in the template.

For some reason, accessing the header using flow variable "message.header.content-type" doesn't return the first value as documented here: https://cloud.google.com/apigee/docs/api-platform/reference/variables-reference#message. So one of these should work but don't:

    <AssignVariable>
        <Name>message.header.content-type</Name>
        <Ref>message.header.content-type</Ref>
    </AssignVariable>
    <AssignVariable>
        <Name>message.header.content-type</Name>
        <Ref>message.header.content-type.1</Ref>
    </AssignVariable>

 I'll look into the reasons for the header indexing issue.

View solution in original post

2 REPLIES 2

Can you ask why they are sending :)? Is there any impact on backend? In apigee we can do in many ways regexp or js..but try simple js to trim & set it using assign message before sending to backend..

var contentTypeList = context.getVariable("request.header.Content-Type").split(",")[0].trim();
context.setVariable("contentType", contentTypeList);

Hi Raghu,

This can happen if the client sends duplicate headers, they get concatenated into a single comma separated header value even if specified using curl as:

curl https://hostname/path -H Content-type:application/json -H Content-type:application/json

You can use an Assign Message to "clean" multiple values:

<AssignMessage continueOnError="false" enabled="true" name="AM-clean-content-type">
    <AssignVariable>
        <Name>regex</Name>
        <Value>application/json,</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>request.header.content-type</Name>
        <Template>{replaceAll(request.header.content-type,regex,'')}</Template>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

The assignment of the string to the "regex" is required as the literal value doesn't work in the template.

For some reason, accessing the header using flow variable "message.header.content-type" doesn't return the first value as documented here: https://cloud.google.com/apigee/docs/api-platform/reference/variables-reference#message. So one of these should work but don't:

    <AssignVariable>
        <Name>message.header.content-type</Name>
        <Ref>message.header.content-type</Ref>
    </AssignVariable>
    <AssignVariable>
        <Name>message.header.content-type</Name>
        <Ref>message.header.content-type.1</Ref>
    </AssignVariable>

 I'll look into the reasons for the header indexing issue.