0

Hey i have an api request and i need the value in payload to by dynamic, i tried with f'' but it wont make it dynamic

will appreciate your help.

import requests

url = "https://www.stie.com/api/conversations/KJQ-CZHNR-985/fields"
valuetarget = "123"

payload = {'apikey': 'zbyc88srdi333d3dpq5lye48pgg1tfo1pnjvj65ld',
           'customfields': '[{"code": "orderno", "value":"valuetarget"}]'}
files = [

]
headers = {}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
3
  • What is your error message? Commented Jun 24, 2022 at 5:50
  • i just cant make valuetarget in the payload dynamic - as it consider it as a string there Commented Jun 24, 2022 at 5:54
  • Can try payload to json.dumps(payload) ? Commented Jun 24, 2022 at 6:07

2 Answers 2

1

You just need to load json and dump it to string and send it.

import json
valuetarget = "123"
payload = {
     'apikey': 'zbyc88srdi333d3dpq5lye48pgg1tfo1pnjvj65ld',
     'customfields': 
          json.dumps([{"code": "orderno", "value":valuetarget}])
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should escape the curly braces in the formatted string, like so:

f'[{{"code": "orderno", "value":"{valuetarget}"}}]'

But why not let requests format the string for you?

import requests

url = "https://www.stie.com/api/conversations/KJQ-CZHNR-985/fields"
valuetarget = "123"

# 'customfields' is now a list of dictionaries
payload = {'apikey': 'zbyc88srdi333d3dpq5lye48pgg1tfo1pnjvj65ld',
           'customfields': [{"code": "orderno", "value": valuetarget}]}
files = [

]
headers = {}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)

1 Comment

Perfect, the escape did the job. Thank you very much :)

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.