title: WSO2 Synapse: setting a URL parameter
body: <p>I am trying to do something that seems straightforward but can't get it to work. Basically I want the WSO2 API manager to add a URL parameter to a REST call.</p>
  
  <h2>Setup and Problem</h2>
  
  <p>I have a WSO2 API manager installed. I also have a Tomcat running with a bogus servlet called <strong>someservlet</strong> that simply logs anything it receives and returns ok. I have added the servlet as an API in the manager, so I can either call the servlet directly or via WSO2 API mgr.</p>
  
  <p>I can run</p>
  
  <pre><code>curl http://localhost:8080/someservlet/servlet/toto?blob=blib&amp;secret=pass
  </code></pre>
  
  <p>and the servlet works fine, it tells me it's received the path <strong>/toto</strong> and parameters <strong>blob</strong> and <strong>secret</strong>.</p>
  
  <p>I can run</p>
  
  <pre><code>curl -H "Authorization: Bearer [...]" --url "http://192.168.23.1:8280/someservlet/1.0/toto?blob=blib&amp;secret=pass"
  </code></pre>
  
  <p>And it does exactly the same. So far, so good.</p>
  
  <p>What I want is to run:</p>
  
  <pre><code>curl -H "Authorization: Bearer MqVQuHqLNphtPV3XF1CtXVmbyP8a" --url "http://192.168.23.1:8280/someservlet/1.0/toto?blob=blib"
  </code></pre>
  
  <p>(note that I've removed the <strong>secret</strong> parameter)</p>
  
  <p>...and still get the same result.</p>
  
  <p>So basically I want API manager to add the URL parameter <strong>secret=pass</strong>.</p>
  
  <h2>First thing I tried: property mediator</h2>
  
  <p>Use Synapse XML config with <strong>property REST_URL_POSTFIX</strong>.</p>
  
  <p>I edited the API config file, and added</p>
  
  <pre><code>&lt;property name="REST_URL_POSTFIX" value="/blob?toto=titi" scope="axis2" type="STRING"/&gt;
  </code></pre>
  
  <p>Now if I run</p>
  
  <pre><code>curl -H "Authorization: Bearer [...]" --url "http://192.168.23.1:8280/someservlet/1.0/toti?blab=blib&amp;secret=puss"
  </code></pre>
  
  <p>it's as if I ran <strong>someservlet/1.0/blob?toto=titi</strong>: all my path and parameters have disappeared and been repaced with the configured ones. Hey, that's how it's supposed to work, isn't it!</p>
  
  <p>Problems:</p>
  
  <ol>
  <li>this doesn't add something to the URL, it sets the URL postfix, meaning that existing parameters disappear (in the above example, <strong>blab=blib</strong>)</li>
  <li>it has to start with a <strong>"/"</strong> to be a valid value, so I can't just add <strong>&amp;secret=pass</strong> (of course, because of problem 1, this would be useless anyway)</li>
  </ol>
  
  <p>So basically this doesn't enable me to append the final <strong>&amp;secret=pass</strong>.</p>
  
  <h2>Second thing I tried: rewrite URL mediator</h2>
  
  <p>I found this mediator, and although it probably won't do the trick, it's a good lead: I can just call with <strong>secret=foo</strong>, and get the mediator to replace it with <strong>secret=pass</strong>.</p>
  
  <p>I put this in the config file:</p>
  
  <pre><code>&lt;rewrite&gt;
    &lt;rewriterule&gt;
      &lt;action type="replace" value="pass" fragment="query" regex="foo"/&gt;
    &lt;/rewriterule&gt;
  &lt;/rewrite&gt;
  </code></pre>
  
  <p>This doesn't work. At first I thought I didn't have the action parameters right. But the error message is:</p>
  
  <pre><code>Malformed URL when processing /someservlet/1.0/toti?blab=blib&amp;amp;secret=foo
  </code></pre>
  
  <p>Malformed? There's more detail in the exception stack trace:</p>
  
  <pre><code>java.net.MalformedURLException: no protocol: /someservlet/1.0/toti?blab=blib&amp;secret=foo
  </code></pre>
  
  <p>So what's happening is that the mediators (log or rewrite) receive a message whose <strong>"To:"</strong> field points to a URL with no protocol!</p>
  
  <p>Of course I've been googling around, and there are some cases where I find other people have <strong>logMediator: To: /blabla</strong>, and other (most) cases where they have <strong>logMediator: To: http ://blabla</strong>. I don't really see what's causing the difference. :-(</p>
  
  <p><strong>So this is where I'm stuck!! :-(</strong></p>
  
  <h2>Option to be tried</h2>
  
  <p>I'm aware that there's probably a sledgehammer solution that should work:</p>
  
  <ol>
  <li>use <strong>property</strong> to store the full path and all parameters</li>
  <li>implement my own mediator (e.g. in Java) to modify these parameters</li>
  <li>use <strong>property REST_URL_POSTFIX</strong> to put the modified postfix on the call</li>
  </ol>
  
  <p>However I feel that this problem should have a simpler solution.</p>
  
  <p>I have a kind of hope that someone will point me to a simple resource (mediator, sample, syntax error, anything) that I haven't found and that does just what I want. Optimism... :-)</p>
  
  <p>Thanks for reading. Any ideas?</p>