How to pass a non-url request parameter to Smalltalk Teapot from Ajax call

Viewed 58

I am trying to create an API in Smalltalk with Teapot, and everything was ok until I needed to update objects with very long content fields. This is an example of my code:

Teapot on
   POST: '/posts/<key>/update' -> [ :req | (posts at: (req at: #key)) updateContent: (req at: #content)];
   start.

If I request the endpoint in this way, it works:

ZnClient new
   url: 'http://localhost:8080/posts/1/update';
   formAt: 'content' put: 'Imagine this is a large content'; 
   post.

It takes the first parameter from the URL and the second from the form data. But the frontend is actually a Web application and it is performing the requests through Javascript.

The first easy attempt was calling the endpoint with url-based params, and it worked, but I got an error when the URL is too large:

'http://localhost:8080/posts/'+id+'/update?content='+encodeUri('Here goes a very large content')

So I used forms, as usual, but the server can't find the 'content' parameter:

 var formData = new FormData();
    formData.append("content", "A large content goes here");
   
 $.ajax({
      "url":'http://localhost:8080/posts/'+id+'/update',
      "type": 'post',
      "processData": false,
      "contentType": false,
      "data": formData,
      "success": function (data) {
          resolve(data);
      },
      "error": function (request, status) {
          reject(request.responseText);
      }
    });

I debugged, but the parameter is not present in the 'req' object. The parameter isn't found at this point:

TeaRequest > formParam: aSymbol ifAbsent: aBlock
    ^ (znRequest entity isKindOf: ZnApplicationFormUrlEncodedEntity) "XXX do something better" 
        ifTrue: [ znRequest entity at: aSymbol ifAbsent: aBlock ]
        ifFalse: aBlock

PS: the headers I'm using are:

   resp headers at: 'Access-Control-Allow-Origin' put: '*'.
    resp headers at: 'Access-Control-Expose-Headers' put: 'X-Total-Count'.

Any idea?

1 Answers

The parameters sent via forms are accessible through the req object. It is not really clear, but I did it by:

(req entity contents at:1) fieldName; fieldValueString.
Related