xml - How to encode python variables into URL and web-server response? -
Assume that I have a python function that takes many arguments in the form of input and returns an output.
Now I want "web-service" (API) based on this function. It means that the URL should be called with some parameters, this parameter should be sent to the function, the function takes them, and calculates the result (output) and returns it back to the web-server In which its turn shows the result to the user (possibly any other program).
In the case of a simple task, two real-valued arguments (for example, As a result I hope that A web page is only a real number. However, it is not so clear to me that what I should do with more complex input. Suppose we have a list and dictionaries in the form of the value of some arguments, how do I encode them in the URL? I can think of something like this: However, I'm not sure that this is a great solution. Is there any standard how to do this? Maybe I should use XML or JSON to encode the input. The same question applies to the output. Suppose we can also do dictionary or list or a set as output, how should I represent it? Should I just use Am I simply should be used! First of all, you want The best solution is to use it to translate into symbolic words. You can serialize and deserialize However, I will use POST instead of GET when sending JSON payloads (although it is safe for URL-encoded JSON data - but there is not a long string of JSON in the URL that is beautiful Female). In this way you can dump the entire JSON structure directly into the requested body, use a If you need to support variable types that are not in JSON, then you can sub-class just JSON encoder / decoder and by changing the type of notification you want to do. For example, An ISO date string for Datetime objects. x and
y ) and returns a real-valued result, ceremony The call may be thrown to the throwing parameters. For example:
www.my_web_site.org/my_func_name?x=1.234&y=3.456
www.my_web_site.org/my_func_name?x= [1,2,3,4] and y = {1: 'a', 2: 'b '}
str (output) or any other "standard" solution is?
str (output) ?
repr () because it returns valid python code for many built-in python types. However, in order to parse its output, you have to execute it somehow and when it is safe to run on untrusted inputs in Python (this is the only string, number, tuples, lists, directories, booleans and
any handles it), it is still not very portable and somewhat ugly is the Python code for data.
json.loads by using
json.dumps to do this.
application / json content-type and you can easily access it in your backend, e.g. If you are using a flask.
Comments
Post a Comment