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, 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   

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:

  www.my_web_site.org/my_func_name?x= [1,2,3,4] and y = {1: 'a', 2: 'b '}   

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 str (output) or any other "standard" solution is?

Am I simply str (output) ?

should be used! First of all, you want 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.


The best solution is to use it to translate into symbolic words. You can serialize and deserialize json.loads by using json.dumps to do this.

  • It can be very parsed in every one language, so if you want to send a request from another tool, then you can easily create the parameter
  • It is standardized format and easily readable by humans.
  • It is safe to unassign untrusted data (you can not write JSON when you are making random and you can not execute arbitrary code such as you can become a mass memory hog )
  • It does not add large-scale bloat / overhard like XML.

    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 application / json content-type and you can easily access it in your backend, e.g. If you are using a flask.

    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.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -