[Libwebsockets] http GET request

andy at warmcat.com andy at warmcat.com
Wed Jun 30 13:38:00 CEST 2021



On June 30, 2021 10:37:37 AM UTC, Avigail Wilk <awilk at kramerav.com> wrote:
>Hi,
>
>What is the right way to implement RestApi using lws with GET request?
>As a response to a GET request I want to return the content of a file
>(and optionally editing the file's content before sending it).
>
>The way It's implemented in my project is:
>when I recognize the Get request in the callback function  (in case
>LWS_CALLBACK_HTTP) I read the file's content, edit it if needed, and
>then write it using lws_write function and returning -1;
>This worked with libwebsockets v2.0 but not with v4.2.0.
>I thought that in v4.2.0 it might be a problem to call lws_write from 
>LWS_CALLBACK_HTTP,
>so instead I used lws_callback_on_writable and in
>LWS_CALLBACK_HTTP_WRITEABLE I used lws_write. But this didn't solve the
>problem.
>
>I don't want to use lws_serve_http_file because I want the option to
>return the content as a string and edit it.
>
>Any idea of what I am doing wrong? Or what is the right way to achieve
>this?

The right way is look through the minimal examples, test those without changes, if it's what you want, or in the right direction for what you want, just cut and paste it  as your starting point.

The examples didn't exist in 2.x era, they will repay the time looking through them many times over.

Eg https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/http-server/minimal-http-server-dynamic/minimal-http-server-dynamic.c

...as Pawel says on line 113 you will find a call to the fimalize api, which helps conceal if the connection is h1 or h2.  But by starting from something that already works end to end, like the examples, you don't have to understand all these details to use it successfully.

-Andy

>Many thanks!
>
>
>This message has been scanned for malware by Websense. www.websense.com


More information about the Libwebsockets mailing list