How to send a list and an image in a post request in python [duplicate] - json

This question already has answers here:
Python Requests: Post JSON and file in single request
(7 answers)
Upload Image using POST form data in Python-requests
(8 answers)
Closed 3 days ago.
I am using python trying to send a post request to an API that I developed in flask.
I want to send an image and a list that looks like this:
Mylist = [(1,2,3,4),(23,15,5,6)...]
My list could contain 0 to many sets (or lists) of 4 integers.
How can I do that?
I tried doing something like
Image=open(urlimage, 'rb')
Res = requests.post(url, data={"list" : Mylist}, files=image)
But in the API I don't get the list in a different format.
Is there no workaround for it?

Related

How to send ndarray response from flask server to python client? [duplicate]

This question already has answers here:
What's the best way to parse a JSON response from the requests library?
(3 answers)
Return JSON response from Flask view
(15 answers)
Closed 3 years ago.
I am trying to send ndarray (predictionsIm) of shape (1536,1536,3) from flask server to the python client.
I read around for converting predictionsIm (ndarray) to list and then return the json, so I did as below:
return {'predictions' : predictionsIm.tolist()}
but I don't know how to handle it on client slide, can someone help in this approach?
or suggest a better approach?
Following is my request.py:
import requests
sample_input = "/mnt/d/work/MASKRCNN/data_example/img0001.png"
resp = requests.post("http://localhost:5000/predict",
files={"file": open(sample_input,'rb')})
print(resp.content)
I think you forgot the json step
return jsonify({'predictions' : predictionsIm.tolist()})

How can i use "/" in rest api url? [duplicate]

This question already has answers here:
slashes in url variables
(4 answers)
Closed 4 years ago.
I have a requirement where we make an api call and it contains "/" in the rest api uri. For example, www.abc.com/books/bookid/1232kkjf/asdis/waew. The bold string is the book id and it contains "/". When we use it, we receive error.
If it is possible to use "/", please help me with some details. Appreciate your help!!
Whenever you have path parameters that comprises of special characters like '/' you must perform URL encoding. While creating your HTTP request you must encode "1232kkjf/asdis/waew" with
If you are using Java Client then use URLEncoder
String myEncodedPathParam = URLEncoder.encode(rawParm, "UTF-8");
I you are using any other client like PostMan then you can use a lot of online tools that convert string to URLEncoded UTF-8 ex. https://www.url-encode-decode.com/
This way / will be replaced by %2F
You need to replace it with '%2F' in the string.
See similar question:
How do I pass the # symbol as part of a GET querystring in the URL?
Also see this link:
https://www.degraeve.com/reference/urlencoding.php

security of `JSON.parse()` in Ruby? [duplicate]

This question already has answers here:
JSON parsing and security
(2 answers)
Closed 5 years ago.
Can I JSON.parse untrusted data and then validate its structure (e.g., map from strings to array of strings)? Or can bad stuff happen just from parsing JSON (like with Marshal, which is inherently unsafe against remote code execution)?
JSON.parse does not evaluate anything, it parses json. What can go wrong? Nothing.

Flask with Backbone.js REST API [duplicate]

This question already has answers here:
Get the data received in a Flask request
(23 answers)
Closed 14 days ago.
Model.save() in backbone.js sends the model data as POST to server as a JSON encoded string. Its just a string and not variable=jsonString as in a normal POST request. So I cant access it in Flask as request.form.get('variable'). Where should i be editing code? Can Flask deal such requests? Can Backbone.js send data like a normal post request and not as a JSON encoded string?
Can provide more info if required.
With Backbone the request Content-Type header is automatically set to 'application/json' (unless you've enabled emulateJSON), so Flask should automatically parse the JSON and make it available through the request object.
#app.route('/some_route', methods=['POST', 'GET'])
def some_route():
if request.method == 'POST':
""" json available through request.json """
http://flask.pocoo.org/docs/api/#flask.Request.json

ASMX returning JSON always inside an xml tag [duplicate]

This question already has answers here:
How to let an ASMX file output JSON
(6 answers)
Closed 9 years ago.
I have asmx services that return json strings. I use DataContractJsonSerializer class and the resulting json is fine.
When I call these services to consume in a web app, the response is always coming backencapsulated in an xml tag. Any ways to get rid of the xml and return only the json.
Example response :
< ?xml version="1.0" encoding="utf-8" ?>
< string xmlns="http://whatever.net/">
{"Success":true,"Data":{"RoomList":[{"CreatedAt":"\/Date(1291192978793-0800)\/","GameTimeOut":0,"MatchPointingType":0,"MoveTimeOut":0,"Name":"Reterras numagona alantis","RoomID":44,"SFID":null,"StartingMatchPoints":0,"UpdateAt":null,"UserID":12},{"CreatedAt":"\/Date(1291115544347-0800)\/","GameTimeOut":0,"MatchPointingType":0,"MoveTimeOut":0,"Name":"12122","RoomID":43,"SFID":null,"StartingMatchPoints":0,"UpdateAt":null,"UserID":2},{"CreatedAt":"\/Date(1291115537413-0800)\/","GameTimeOut":0,"MatchPointingType":0,"MoveTimeOut":0,"Name":"12122","RoomID":42,"SFID":null,"StartingMatchPoints":0,"UpdateAt":null,"UserID":2}],"TotalCount":42}}
< / string>
Sounds similar to this question:
How to let an ASMX file output JSON