5

I'm building an API with WCF (C#) and testing it with Postman. I seem to be having trouble using the "Params" section within Postman, as it is translating any key value pairs I input into Query String Params.

My Contract specifies the UriTemplate like so...

    [OperationContract]
    [WebGet(UriTemplate = "/GetClientDataFromAlias/Alias/{alias}", 
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json)]
    GetClientDataFromAliasResponse GetClientDataFromAlias(string alias);

Yet when I run the request through Postman the URL is translated into the following...

http://troikawcf.localhost/ClientWCFService.svc/GetClientDataFromAlias?Alias=myalias

What I would like it to translate to is the following, to match my contract

http://troikawcf.localhost/ClientWCFService.svc/GetClientDataFromAlias/Alias/myalias

Am I missing a setting in Postman to set all params in the Path format? Or do I need to change my Contract to utilize Query String Params?

See screen grab below for more info...

Many Thanks

enter image description here

grimdog_john
  • 605
  • 6
  • 14

3 Answers3

9

I figured out a way to get this working.

You can use placeholders in the URL bar within Postman that enable you to still utilize the "Params" section to pass through the actual values. This is extremely helpful for quick editing of param values without parsing through the URL. You can just set your URL structure in the Address Bar and let the "Params" Section do the rest.

To do this, you need to add your Path Parameter to the URL as you would do normally, but instead of adding a value, add a placeholder prefixed with a colon. The Placeholder will now appear automatically in the "Params" Section within the 'Key' column. You can add in the actual parameter value in the 'Value' column so that when you run your request the placeholder will get swapped out with the actual value.

Here's an example :- http://troikawcf.localhost/ClientWCFService.svc/GetClientDataFromAlias/Alias/:Alias

An here's an screen capture of Postman to further clarify :-

Path Params in Postman I hope this helps someone else out in the future as it baffled me for days why a REST client would not have support for Path Params.

grimdog_john
  • 605
  • 6
  • 14
1

I believe you want to Creating cURL commands in Postman. hope you can find the answer here. also consider using Fiddler4 in order to test the restful services.

Mohammad
  • 2,482
  • 5
  • 24
  • 48
  • 1
    Hi thanks for your answer - This is really useful to know - but it still doesn't allow me to actually test my REST API through Postman. I didn't know you could generate the code in Postman like this though - so many Thanks for sharing the tip :-) – grimdog_john Apr 13 '17 at 10:20
1

A UriTemplate has basically two parts (from MSDN):

  • Path

A path consists of a series of segments delimited by a slash (/). Each segment can have a literal value, a variable value (written within curly braces [{ }], constrained to match the contents of exactly one segment)

  • An optional query

The query expression can be omitted entirely. If present, it specifies an unordered series of name/value pairs. ... Unpaired values are not permitted.


So, there are basically two ways to include parameters in the URL for a webrequest:

Thesa are the variables in the curly braces that appears before the query and would be like: GetClientDataFromAlias/{alias}

These are the name/value pairs that appears in the optional query expression and would be like GetClientDataFromAlias?Alias=myalias

In your case, when using the Params section in Postman, Postman will add the key/value parameters to the query expression, NOT the path segment because the query expression consists of

an unordered series of name/value pairs.

If you want your parameters to be in the path segment, you need to remove the key/value parameter in Postman and add the parameter in the URL of the request within curly braces

S.Dav
  • 2,316
  • 15
  • 22
  • I understand what you are saying - but I would prefer not to have to use query string params if possible. I would much rather leave my UriTemplate intact and use path variables. If i have to change to get it working through Postman this may have to be the route I take. Many Thanks. – grimdog_john Apr 13 '17 at 15:16
  • FYI - When I specify the params directly in the URL bar in postman (i.e. I don't use the "Params" drop down to add my parameters) everything works OK. It's only when I add them in the "Params" drop down that things stop working. This leads me to believe my Contract is all OK and Postman doesn't fully support Path Variables. I'll keep looking. Thanks again for your info. – grimdog_john Apr 13 '17 at 15:18
  • It has actually nothing to do with Postman, this is the correct behavior. In `UriTemplate`, anything before a (optional) question mark, is part of the URL not a parameter. Query parameters are those after the (optional) question mark. – S.Dav Apr 13 '17 at 17:29
  • 1
    Please refer to this question http://stackoverflow.com/questions/4024271/rest-api-best-practices-where-to-put-parameters. There are two different ways to communicate parameters via REST API's. In the question my preference is path parameters which is option 1 in the question. What you refer to is option 2. – grimdog_john Apr 13 '17 at 18:49
  • @grimdog_john I realized that my answer was a bit confusing so I edited my answer to include a more detailed explanation. – S.Dav Apr 14 '17 at 09:00