Post file in Network

72 views
Skip to first unread message

benjip...@gmail.com

unread,
Feb 2, 2023, 5:05:02 AM2/2/23
to GAMA
Hello,

I have tried many options to upload a file to a server using the POST method but I cannot find the correct syntax in GAMAL. I can't find any examples and not much documentation for GAMAL on this. 

Do you have an example of how I could do this ?

Benoit Gaudou

unread,
Feb 3, 2023, 5:38:45 AM2/3/23
to gama-p...@googlegroups.com
Dear,

I admit we had not such a case for the moment.
What is done is to send some information in POST : 

do send to: "/api/user/" contents: ["POST",["toto"::34,"titi"::world], ["Content-Type"::"application/json"] ];

So I think it will depend on your file and perhaps also on you server side...

If you have more specificity about what you need and details about your architecture, it could ne interesting to add the feature in GAMA.


Cheers


Benoit


--
You received this message because you are subscribed to the Google Groups "GAMA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gama-platfor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gama-platform/2247e1d1-9f53-462a-91f3-6365f7c8bb8en%40googlegroups.com.

benjip...@gmail.com

unread,
Feb 3, 2023, 11:44:30 AM2/3/23
to GAMA
Dear,

My main problem is the json serializer that prepares the body of the request, for example, if I want to do:

do send to: "/api/user/" contents: ["POST","raw-query-param", ["Content-Type"::"application/json"] ];

the parser is going to add quotations to the body raw-query-param and turn it to "raw-query-param", with quotations included.
That means there is no possibility of sending HTTP requests with simple strings, because the parser will add quotation marks, thus modifying them.

Would there be a way to send a POST request with a simple string without the body parser modifying it ?

Cheers

Benjamin

Benoit Gaudou

unread,
Feb 4, 2023, 1:11:16 PM2/4/23
to gama-p...@googlegroups.com
Dear,

I just updated the send method in order that the content may not be modified: you need to specify that the "Content-Type" is "text/plain" (available on the source code developer version).
In this case, the raw-query-param will not be jsonified, so extra quote should not be added.

// do send to: "/api/user/" contents: ["POST","raw-query-param", ["Content-Type"::"text/plain"] ];  


Please send feedback if it works how you want, and if it makes sense for you to rely on the content-type to jsonify or not.

Cheers

Benoit


benjip...@gmail.com

unread,
Feb 6, 2023, 4:13:58 AM2/6/23
to GAMA
Dear,

Thank you for your prompt response.

If I understand correctly, this requires setting the MIME Type to "text/plain." 
However, in my case, I first need to transfer a ".rdf" file that follows the ".xml" standards. In this case, I am obligated to define the MIME Type as "application/rdf+xml." The problem is that an XML file must start with a line like this: "<?xml version='1.0' encoding='UTF-8'?>" and there must not be any characters before this line. Wrapping it in XML returns the error "content is not allowed in prolog" because it finds the quotation mark at the beginning of the document. I was able to circumvent the wrapping by using a ".json-ld" file that complies with the ".xml" standard and is not serialized as "json," but this is not the best solution for me because this type of file is rarely used. 

I then need to run a series of "HTTP" commands, again the MIME Type cannot be "text/plain" but must be "application/x-www-form-urlencoded." In this case, it is the same thing, the server finds quotation marks in the request around the body because it is serialized and therefore cannot find all the necessary parameters for its operation.

There are as many "MIME Types" as there are data formats that can be sent in a request, and I think that in many cases adding quotes can be a problem. I don't think that serialization with quotes is of interest when the "http" protocol is used. 
In what case is it important for you to do serialization and add quotes?

Benoit Gaudou

unread,
Feb 9, 2023, 9:38:56 AM2/9/23
to gama-p...@googlegroups.com
Dear,

I simplify following your idea :
so basically the body is interpreted as a string (whatever the content-type is)
if the modeler wants to send it in a JSON format, it needs to transform it explicitly using the as_json_string operator.

Is it ok for you ?

Cheers

Benoit 

benjip...@gmail.com

unread,
Feb 10, 2023, 4:10:34 AM2/10/23
to GAMA

Dear,

I think it could work. I'll give you two examples that I've worked on as examples:

I need to send a file containing text (xml format) to my server, the HTTP request to use is as follows:


                      POST Content-Type:application/rdf+xml file_content http://localhost...

The file_content is noted in its entirety in the request and without quotes. In GAMA, I should enter the following code:


                     do send to: /url contents["POST", file_content, ["Content-Type"::"application/rdf+xml"];

The file_content is a string type variable that I would have obtained by exploiting my file.

I then wish to create a dataset on my server, the HTTP request to use in this case is:

 
                     POST Content-Type:application/x-www-form-urlencoded dbType=mem&dBName=DB http://localhost...

In this case, the content sent will allow creating a memory dataset with the name DB. In GAMA


                    do send to: /url contents["POST", "dbType=mem&dBName" ["Content-Type"::"application/x-www-form-urlencoded"]];

The database creation parameters are given as a string.

In both cases, GAMA must create and send a request exactly as the HTTP request so that the server can process it, without adding quotes around the string values because the HTTP protocol will not understand them.

In all HTTP requests, regardless of the parameter or file, the processing should be the same.

If, using serialization only on JSON files when the user uses the "as_json_string" operator, it allows to obtain this then I think this solution is the correct one.


I hope my explanations were clear enough, 

Thank you for your help.


Cheers,

 Benjamin

Benoit Gaudou

unread,
Feb 10, 2023, 6:39:40 AM2/10/23
to gama-p...@googlegroups.com
Dear,

OK perfect.
Do not hesitate to come back to us, if you notice some unexpected behaviors.

Cheers

Benoit

benjip...@gmail.com

unread,
Feb 13, 2023, 8:41:58 AM2/13/23
to GAMA
Dear,

Thank you very much for these modifications, the HTTP POST requests work perfectly whether to create the database or to upload a file (xml in my case).
However, I am encountering a small problem with the GET method (which works on the classic GAMA version). In fact, in this version, a GET method works well in the following example you gave:

                              do send to: "/search/authors.json?q=j%20k%20rowling" contents: ["GET"];

In the case of a more complicated request, I would need to specify a header like this:

                              do send to: "url" contents: ["GET", ["Content-Type"::"application/x-www-form-urlencoded", "Accept"::"application/rdf+xml"]];

However, in this case the "send" method does not send the message, because I think it is waiting for an additional "body" argument. I managed to make this work by adding an empty body which is not then used:

                              do send to: "url" contents: ["GET", "", ["Content-Type"::"application/x-www-form-urlencoded", "Accept"::"application/rdf+xml"]];

Since GET requests worked well before, I assume that the modification you made has inadvertently impacted the "do send" method in this specific case.
This results in a more complex syntax for an unused argument, however it works, so it's not a major problem, it could be improved or indicated in the documentation like this.

Do you know approximately when version 1.9 of GAMA will be deployed so that these network capabilities are available for those who are not using the developer version?

Thank you very much for your help, and good luck with the rest of the development.

Cheers

Benjamin

Benoit Gaudou

unread,
Feb 14, 2023, 1:25:05 AM2/14/23
to gama-p...@googlegroups.com
Dear,
You are perfectly right.

Given all these cases, I updated the code to constrain the message content depending on the HTTP method :
- GET or DELETE only accept [method] or [method,headers]
- POST, PUT only accept [method, body] or [method,body,headers]
The last commit should fix your issue.

I admit I cannot tell you when the 1.9 will be released (hopefully in the coming months).
Meanwhile, there is always the option of downloading the alpha releases (https://github.com/gama-platform/gama/releases) that are generated from time to time.
I can run the build of one to contain all the HTTP updates.

Cheers

Benoit

benjip...@gmail.com

unread,
Feb 15, 2023, 9:45:21 AM2/15/23
to GAMA
Dear,

I think it can be good for me to have an alpha release with the HTTP updates.
I continue to develop my program and i tell you if there are another problem with HTTP requests.
Tanks you for your modification and good luck for the 1.9 release.

Cheers

Benjamin

Reply all
Reply to author
Forward
0 new messages