[Libwebsockets] [EXTERNAL] Re: http GET request

Avigail Wilk awilk at kramerav.com
Wed Jun 30 17:33:23 CEST 2021

The answer Pawel sent me solved the problem.
The minimal examples help a lot to understand the library. Thanks!
Of course I looked through the minimal examples before asking... but there are so many of them so I didn't look at the one that would help me.
Every example clarifies more points that I missed, so thanks a lot for providing them!!

-----Original Message-----
From: andy at warmcat.com <andy at warmcat.com> 
Sent: Wednesday, June 30, 2021 2:38 PM
To: Avigail Wilk <awilk at kramerav.com>; libwebsockets at ml.libwebsockets.org
Subject: [EXTERNAL] Re: http GET request

On June 30, 2021 10:37:37 AM UTC, Avigail Wilk <awilk at kramerav.com> wrote:
>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 
>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 

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.


>Many thanks!
>This message has been scanned for malware by Websense. www.websense.com
CAUTION:This email originated from outside of the organization.
Do not click links or open attachments unless you recognize the sender and know the content is safe.

More information about the Libwebsockets mailing list