Flask or Werkzeug/0.9.4 breaking POST data - python

I have found some behaviour of Flask-restful caused I think by Werkzeug/0.9.4 that I do not understand. It seems that the use of Multidict is breaking my data when I try to POST valid JSON that contains a "=".
Here's my test JSON:
{
"alert": {
"#id": "90",
"action": "hoojimaflip",
"fruit": {
"#bowl": "bananas",
"#protocol": "tcp"
},
"url": "https://this-is-a-sample/paramer?id=90"
}
}
Here is the POST method.
def post(self):
f1=open("./log", 'w+')
data = request.json
if not data:
# I know this is not equivalent to the JSON above.
# Just troubleshooting by dumping it all out.
data = request.form
print >>f1, data
return ('', 201)
If I POST using cURL with application/json it's fine. I get the POSTed JSON correctly in request.data. I will need to render it back to JSON later, but no issue.
{
u'alert': {
u'#id': u'90'
u'action': u'hoojimaflip',
u'fruit': {
u'#bowl': u'bananas',
u'#protocol': u'tcp'
},
u'url': u'https://this-is-a-sample/paramer?id=90',
}
}
If I post via cURL with application/x-www-form-urlencoded, then I should be able to get the data in request.form. But, it seems that something is breaking my data.
ImmutableMultiDict([('
{ "alert": {
"#id": "90",
"action": "hoojimaflip",
"fruit": {
"#bowl": "bananas",
"#protocol": "tcp"
},
"url": "https://this-is-a-sample/paramer?id', u'90"
}
}'
)])
The "=" sign is being used as some kind of record separator and breaking the POSTed JSON.
Does anyone have any ideas? Am I missing something obvious?
Thanks!

If an external application is stubbornly POST-ing with an alternative mime-type, you can force Flask to treat the data as JSON anyway by using the request.get_json() method instead, setting the force argument to True:
data = request.get_json(force=True)
Don't try to treat a JSON payload as form data, that'll never work.

Related

Cloudflare KV API Bulk PUT - Error 10012 - could not unmarshal KVPair into intermediate struct

Writing a bulk PUT to the CF Workers KV in Python and can’t figure out what’s wrong here. My JSON is valid as far as I can tell.
Here is the error I’m getting
Error
{ "result": null, "success": false, "errors": [ { "code": 10012, "message": "could not parse array of key/value objects from request body: 'could not unmarshal KVPair into intermediate struct: 'json: cannot unmarshal object into Go struct field kvPairJSON.value of type string''" } ], "messages": [] }
Payload
[{ “key”:“aals”, “value”:{ “sup”:{ “firo”:“aals”, “mean”:“aals”, “alpha”:[]}}}]
Code
response = requests.put(f"{CF_BASEURL}/bulk", headers=headers, data=json.dumps(payload))
Any ideas appreciated. I'm sure this is a SMH moment...
That's a useless error message, sorry about that. Workers KV values are plain bytes that may be deserialized in a Worker if needed (e.g. parsed as JSON). I think what you want to do there is just,
import json
payload = [{ "key": "aals", "value": json.dumps('{"sup": {"firo": "aals", "mean": "aals", "alpha": []}}')}]
So that your value is encoded as a string. And then in your Worker you can,
let jsonValue = KV_NAMESPACE.get("aals", "json");

How to pass the parameter in JSON to append single data in list (Documentum REST API) POSTMAN

I am getting result from GET API as http://localhost:8009/dctm-rest/repositories/myrepo1/groups/group1 as below in JSON :
{
"name": "group",
"type": "dm_group",
"definition": "http://localhost:8009/dctm-rest/repositories/myrepo1/types/dm_group",
"properties": {
"group_name": "group1",
"users_names": [
"Ben Afleck",
"Jason Rana "
],
I want to add user Vikas Rana under the users_names column and I am giving below parameter in the POST Method in postman as :
{
"properties":
{
"users_names": ["Vikas Rana"]
}
}
But it is replacing the all the names with Vikas Rana. It should be appended as it is a list.
Please help how to pass the parameter in JSON to append the data in list.
Firstly, you need to define a PUT or PATCH method on your end point. You cannot send a POST/PATCH/PUT request to an endpoint view that only handles GET requests.
Tell me about your app so I can help you better. What package/framework are you using to define your API methods? What is your data base and how are you connecting to it?

JSON Parsing issue - Python

So i have build a REST Client that returns JSON response. However, i have an issue, where the JSON output is not exactly what i need:
Current Response:
{
"output": {
"status": "Device 'Test' does not exist",
"result": "null",
"response": {
"output": "success",
"result": 204
}
}
}
This output has an outermost enclosing "output" key, but i don't want that to be present. So basically i want my response to look like below:
{
"status": "Device 'Test' does not exist",
"result": "null",
"response": {
"output": "success",
"result": 204
}
}
I did try converting the JSON to Dict and then remove it, but no luck? any suggestions how to achieve this?
Thank you
if your response is already a dictionary or a json object then you can do following
value_required = response["output"]
if it is in text format (which I think it is) then you just need to do the following
import json
value_required = json.loads(response)["output"]
You should be able to do:
response = json.loads(response)['output']

Eve with Postman "must be of list type" error

I have an eve schema set up as such:
schema = {
"month": {
"type": "datetime",
"required": True,
},
"test": {
"type": "list"
},
}
And I'm using postman to make a post request:
I'm at a bit of a loss here as to why I'm getting this error, am I missing something glaringly obvious? Is that not the correct way to format a list for postman/eve?
Other fields work fine, datetimes, strings, integers, etc. But as soon as I try to post a list, no matter what I do I get this error.
If you check the request being sent by Postman using form-data body, you can see that the body goes similar to this:
------WebKitFormBoundarynhX0dI6JZNPzq8AK
Content-Disposition: form-data; name="month"
2017-08-01T00:00:00
------WebKitFormBoundarynhX0dI6JZNPzq8AK
Content-Disposition: form-data; name="test"
[1,2,3,4,5,6,7]
------WebKitFormBoundarynhX0dI6JZNPzq8AK--
Using raw body from Postman, and setting the Request-header Content-type to application/json, it goes like this, and works for eve:
{
"month": "2017-08-01T00:00:00",
"test": [1,2,3,4,5,6,7]
}
I can't give you a good explanation about why, but this is how I do to make it work with pyeve.

What is wrong with my patch request to the Connectwise Rest API?

I've got a question about the Rest API for Connectwise. I've been doing get and post requests with no issue, but when I do a patch request I get a 400 response with 'field value is invalid' message regardless of what I try. I'm on 2016v1 and using the Rest API making calls from Python with the requests library.
The Rest API documentation says the following object is supposed to be passed in the body, but I haven't a clue what values are supposed to go with these keys:
{
op (string, optional),
path (string,optional),
value (string,optional)
}
I've tried dozens of calls including with the following bodies:
{'summary': 'updatedsummarytext'}
{'value': {'summary': 'updatedsummarytext'}}
{'op': {'summary': 'updatedsummarytext'}}
I have only gotten the following response so far:
<Response [400]>
{
"code": "InvalidObject",
"message": "operations object is invalid",
"errors": [
{
"code": "InvalidField",
"message": "The field value is invalid.",
"resource": "operations",
"field": "value"
}
]
}
Is their a specific value that connectwise is expecting for the op or value keys, or is there something I'm missing unique to Patch rest api calls?
The PATCH calls at a basic level use RFC6902.
Consider the following (simplified) Ticket document:
{
"summary": "Initial Ticket Summary",
"id": 1,
"company": {
"id": 5
},
"board": {
"id": 10
}
}
If you wish to update the summary field, your PATCH request JSON would look like this:
[
{"op": "replace", "path": "/summary", "value": "Updated Summary"}
]
Hope this helps.

Categories