简体   繁体   English

如何在 URL 请求中将参数传递给有效载荷(python)

[英]How to pass a parameter to the payload in a URL request (python)

( https://i.stack.imgur.com/xgoMx.jpg ) I want to be able to pass a variable through to where the registration plate (AA19AAA) is ( https://i.stack.imgur.com/xgoMx.jpg ) 我希望能够将变量传递到车牌 (AA19AAA) 所在的位置

Overall, I want a function to check the number plate, therefore I need to pass a parameter through the function and use it in the payload, but I'm not sure how to actually pass a string to the inside of the payload.总的来说,我想要一个 function 来检查车牌,因此我需要通过 function 传递一个参数并在有效载荷中使用它,但我不确定如何将字符串实际传递到有效载荷内部。

I have tried using.format(...) to add things to the string, concatenating the variable into the payload, and another method similar to.format(...).我尝试过使用.format(...) 向字符串添加内容,将变量连接到有效负载中,以及另一种类似于.format(...) 的方法。 However, if you believe any of these could be applicable, please don't hesitate to tell me as I may have made a mistake somewhere.但是,如果您认为其中任何一个可能适用,请不要犹豫告诉我,因为我可能在某处犯了错误。

I hope some of you can assist me in this, I'm sorry if there is an incredibly simple solution to this, but I have tried a few, and feel that it is time to ask the professionals.我希望你们中的一些人能在这方面帮助我,如果有一个非常简单的解决方案,我很抱歉,但我已经尝试了一些,觉得是时候请教专业人士了。 Thank you so much太感谢了

you have 2 options:你有两个选择:

import requests
import json
payload = {'registrationNumber': some_var}
headers = {..., 'content': 'application/json'}
result = requests.post(url, headers = headers, data=json.dumps(payload))

or或者

import requests
headers = {...}
payload = {'registrationNumber': some_var}
result = requests.post(url, headers=headers, json=payload)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM