PUT dictionary in dictionary in Python requests

2024/5/20 15:08:56

I want to send a PUT request with the following data structure:

{ body : { version: integer, file_id: string }}

Here is the client code:

def check_id():id = request.form['id']res = logic.is_id_valid(id)file_uuid = request.form['file_id']url = 'http://localhost:8050/firmwares'r = requests.put(url = url, data = {'body' : {'version': id, 'file_id': str(file_uuid)}})

Here is the server code:

api.add_resource(resources.FirmwareNewVerUpload, '/firmwares')class FirmwareNewVerUpload(rest.Resource):def put(self):try:args = parser.parse_args()except:print traceback.format_exc()print 'data: ', str(args['body']), ' type: ', type(args['body'])return

The server prints:

data:  version  type:  <type 'unicode'>

And this result is not what I want. Instead of inner dictionary I got a string with name of one dictionary key. If I change 'version' to 'ver'

    r = requests.put(url = url, data = {'body' : {'ver': id, 'file_id': str(file_uuid)}})

server prints

data:  ver  type:  <type 'unicode'>

How to send a dictionary with inner dictionary?

Answer

Use json= instead of data= when doing requests.put and headers = {'content-type':'application/json'}:

 r = requests.put(url = url, json = {'body' : {'version': id, 'file_id': str(file_uuid)}}, headers = {'content-type':'application/json'})
https://en.xdnf.cn/q/72786.html

Related Q&A

Does python have header files like C/C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

python: Greatest common divisor (gcd) for floats, preferably in numpy

I am looking for an efficient way to determine the greatest common divisor of two floats with python. The routine should have the following layoutgcd(a, b, rtol=1e-05, atol=1e-08) """ Re…

Difference between @property and property()

Is there a difference betweenclass Example(object):def __init__(self, prop):self._prop = propdef get_prop(self):return self._propdef set_prop(self, prop):self._prop = propprop = property(get_prop, set_…

airflow webserver command fails with {filesystemcache.py:224} ERROR - Operation not permitted

I am installing airflow on a Cent OS 7. I have configured airflow db init and checked the status of the nginx server as well its working fine. But when I run the airflow webserver command I am getting …

Django REST Framework - How to return 404 error instead of 403

My API allows access (any request) to certain objects only when a user is authenticated and certain other conditions are satisfied.class SomethingViewSet(viewsets.ModelViewSet):queryset = Something.obj…

503 Reponse when trying to use python request on local website

Im trying to scrape my own site from my local server. But when I use python requests on it, it gives me a response 503. Other ordinary sites on the web work. Any reason/solution for this?import reques…

Dereferencing lists inside list in Python

When I define a list in a "generic" way:>>>a=[[]]*3 >>>a [[],[],[]]and then try to append only to the second element of the outer list:>>>a[1].append([0,1]) >>…

Unit test Theories in Python?

In a previous life I did a fair bit of Java development, and found JUnit Theories to be quite useful. Is there any similar mechanism for Python?Currently Im doing something like:def some_test(self):c…

how to do a nested for-each loop with PySpark

Imagine a large dataset (>40GB parquet file) containing value observations of thousands of variables as triples (variable, timestamp, value).Now think of a query in which you are just interested in …

Understanding an issue with the namedtuple typename and pickle in Python

Earlier today I was having trouble trying to pickle a namedtuple instance. As a sanity check, I tried running some code that was posted in another answer. Here it is, simplified a little more:from coll…