How to POST JSON data with Python Requests?

Starting with Requests version 2.4.2, you can use the json= parameter (which takes a dictionary) instead of data= (which takes a string) in the call: >>> import requests >>> r = requests.post(‘http://httpbin.org/post’, json={“key”: “value”}) >>> r.status_code 200 >>> r.json() {‘args’: {}, ‘data’: ‘{“key”: “value”}’, ‘files’: {}, ‘form’: {}, ‘headers’: {‘Accept’: ‘*/*’, ‘Accept-Encoding’: ‘gzip, deflate’, ‘Connection’: … Read more

How to use Jackson to deserialise an array of objects

First create a mapper : import com.fasterxml.jackson.databind.ObjectMapper;// in play 2.3 ObjectMapper mapper = new ObjectMapper(); As Array: MyClass[] myObjects = mapper.readValue(json, MyClass[].class); As List: List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){}); Another way to specify the List type: List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));

Returning JSON from a PHP Script

While you’re usually fine without it, you can and should set the Content-Type header: <?php $data = /** whatever you’re serializing **/; header(‘Content-Type: application/json; charset=utf-8′); echo json_encode($data); If I’m not using a particular framework, I usually allow some request params to modify the output behavior. It can be useful, generally for quick troubleshooting, to not … Read more

Use of PUT vs PATCH methods in REST API real life scenarios

NOTE: When I first spent time reading about REST, idempotence was a confusing concept to try to get right. I still didn’t get it quite right in my original answer, as further comments (and Jason Hoetger’s answer) have shown. For a while, I have resisted updating this answer extensively, to avoid effectively plagiarizing Jason, but … Read more

Parsing JSON with Unix tools

There are a number of tools specifically designed for the purpose of manipulating JSON from the command line, and will be a lot easier and more reliable than doing it with Awk, such as jq: curl -s ‘https://api.github.com/users/lambda’ | jq -r ‘.name’ You can also do this with tools that are likely already installed on … Read more

How to overcome “datetime.datetime not JSON serializable”?

My quick & dirty JSON dump that eats dates and everything: json.dumps(my_dictionary, indent=4, sort_keys=True, default=str) default is a function applied to objects that aren’t serializable. In this case it’s str, so it just converts everything it doesn’t know to strings. Which is great for serialization but not so great when deserializing (hence the “quick & … Read more

How to make a class JSON serializable

Here is a simple solution for a simple feature: .toJSON() Method Instead of a JSON serializable class, implement a serializer method: import json class Object: def toJSON(self): return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4) So you just call it to serialize: me = Object() me.name = “Onur” me.age = 35 me.dog = Object() me.dog.name = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)