How to set response as text/plain using JS in Apigee

Hello all, 

I am working on Non-target API proxy and I am mocking the response for that. Original response have content-type as text/plain and without double quotations, but my response is in application/json with double quotes.

I gave the headers in AM as text/plain but the JS script is not working as expected. 

var Response = "ABC";

context.setVariable("Response",JSON.stringify(Response));

AM Response - 

<set>

<Payload content-Type="text/html" variablePrefix="@" variableSuffix="#">

@codeResponse#

</Payload>

<Headers>

<Header name ="Content-Type">text/plain</Header>

</Headers>

</Set>

Please help me with the Response content to set it as text/plain instead of Json

Solved Solved
0 1 1,659
1 ACCEPTED SOLUTION

I think you want EITHER JS or AssignMessage, you probably do not need both.

AssignMessage

 

<AssignMessage name='AM-Set-Mock-Response'>
  <Set> 
    <Payload contentType='text/plain'>{variable-containing-text-content}</Payload>
  </Set> 
</AssignMessage> 

 

or in JavaScript

 

var textOfResponse = 'whatever you like here';
  -OR-
var textOfResponse = context.getVariable('name-of-variable-holding-text');

context.setVariable('response.content', textOfResponse);
context.setVariable('response.header.content-type', 'text/plain');
 

 

In the AssignMessage you need not also use the Headers/Header element to set the content-type header. It will get set implicitly if you add the contentType attribute to the Payload element. See my example.

Also you don't need the @ and #. The default is to surround referenced variables in curly braces; that default will work fine for your purposes.

Also, I don't know what this means:

Original response have content-type as text/plain and without double quotations, but my response is in application/json with double quotes.

But maybe if you try one of my above suggestions your double-quoting problem will go away.

View solution in original post

1 REPLY 1

I think you want EITHER JS or AssignMessage, you probably do not need both.

AssignMessage

 

<AssignMessage name='AM-Set-Mock-Response'>
  <Set> 
    <Payload contentType='text/plain'>{variable-containing-text-content}</Payload>
  </Set> 
</AssignMessage> 

 

or in JavaScript

 

var textOfResponse = 'whatever you like here';
  -OR-
var textOfResponse = context.getVariable('name-of-variable-holding-text');

context.setVariable('response.content', textOfResponse);
context.setVariable('response.header.content-type', 'text/plain');
 

 

In the AssignMessage you need not also use the Headers/Header element to set the content-type header. It will get set implicitly if you add the contentType attribute to the Payload element. See my example.

Also you don't need the @ and #. The default is to surround referenced variables in curly braces; that default will work fine for your purposes.

Also, I don't know what this means:

Original response have content-type as text/plain and without double quotations, but my response is in application/json with double quotes.

But maybe if you try one of my above suggestions your double-quoting problem will go away.