0

I've got an API request to make that involves passing some variables from user input and a config file to a filter expression contained in a dictionary.

The API uses hashes in its structure to wrap stings by default, although I can specify another string wrapping indicator if need be via a separate request. As is, what I need to do is below, basically.

I can't figure out the syntax to get those strings to populate the values between the wrapper # signs. Lots of questions about this, but none addressing the basic syntax without additional functionality, as far as I can tell.

import config
import requests

var1 = **the result of user input, a string**
var2 = **a value from a config file, also a string**

url = (config.api_url)
payload = {
'key':config.api_key,
'Operation':'GetEntities',
'Entity':'my_entity',
'Attributes':'my_attribute1,my_attribute2',
'Filter':'api_var1<eq>#var1# AND api_var2<eq>#var2#'}

response = requests.post(url,payload)

They key point is here:

'Filter':'api_var1<eq>#var1# AND api_var2<eq>#var2#'

So if var1 = '1234' and var2 = '4321' I need it to be the equivalent of:

'Filter':'api_var1<eq>#1234# AND api_var2<eq>#4321#'
3
  • 'api_var1<eq>{} AND api_var2<eq>{}'.format(var1, var2)? Or even f'api_var1<eq>{var1} AND api_var2<eq>{var2}' with python 3.6. Commented Feb 13, 2018 at 19:23
  • Is that format documented by the API provider? Is there an escape sequence? Is there a provision to avoid malicious data injection? (Consider what happens if var1 contains a hash character.) Commented Feb 13, 2018 at 19:23
  • I'll pass that along to the developer, but yeah, that's the structure they dictate for filters, etc - they are probably expecting hardcoded values here, I wouldn't really know Commented Feb 13, 2018 at 19:36

1 Answer 1

3

As far as I understand you want something like

'Filter':'api_var1<eq>#{0}# AND api_var2<eq>#{1}#'.format(var1, var2)}

or

'Filter':'api_var1<eq>#%s# AND api_var2<eq>#%s#' % (var1, var2)}
Sign up to request clarification or add additional context in comments.

1 Comment

perfect, the first option works, thanks! I'd tried that already but was using '{0}' (with the single quotes) and was getting a syntax error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.