HTTP client in C

I am writing a small library to send HTTP requests but I am stumbling upon many problems one of which is that HTTP 1.0 does not allow more than one request on the same request unlike HTTP 1.1 another problem is that trying to send a GET request to this pastebin /FLa321fb but I keep getting 301 moved permanently but i can access the same link in the web browser

the request used in c code

char * request = "GET /raw/FLa321fb HTTP/1.0\r\nHost: pastebin.com\r\npragma: no-cache\r\nreferer: https://pastebin.com/FLa321fb\r\nUser-Agent:  Mozilla/5.0 (Windows NT 6.2; rv:50.0) Gecko/20100101 Firefox/50.0\r\n\r\n";

the c code output is

result: HTTP/1.1 301 Moved Permanently
Date: Thu, 28 Apr 2022 08:52:07 GMT
Connection: close
Cache-Control: max-age=3600
Expires: Thu, 28 Apr 2022 09:52:07 GMT
Location: https://pastebin.com/raw/FLa321fb
Server: cloudflare
CF-RAY: 70....-AMS

I am trying to find a way to make the c code follow the redirects, obviously adding headers does not seem to make a difference

Greetings,
Unless you're wanting to create this from 'scratch' for a personal reason, I would suggest
installing 'CURL' from your system package manager. You may see references to it also
spelled 'cURL' as well, and that's explained in a number of places as well as their website.
After that get the library source code here:
GitHub - curl/curl: A command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP. libcurl offers a myriad of powerful features
Just perusing the code in this library is an experience that I won't ever forget! It has a Lot of example code too! It's arguably the best library of it's kind on the planet, and Widely used.
The package also contains the CURL command line binary. The website provides an incredible source of documentation, and also has a Tutorial for the library, It's an Education in network protocol programming in C! You can't go wrong checking out that website!

Your network programming will reach a new order of magnitude for the better! ;^)

3 Likes

FWIW, wget also works fine:

MacStudio$ wget https://pastebin.com/raw/FLa321fb
--2022-10-10 16:38:45--  https://pastebin.com/raw/FLa321fb
Resolving pastebin.com (pastebin.com)... 172.67.34.170, 104.20.67.143, 104.20.68.143
Connecting to pastebin.com (pastebin.com)|172.67.34.170|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
Saving to: ‘FLa321fb’

FLa321fb                                    [ <=>                                                                         ]   4.16K  --.-KB/s    in 0s      

2022-10-10 16:38:45 (16.3 MB/s) - ‘FLa321fb’ saved [4261]

MacStudio$ 
1 Like

I didn't mean to demean wget by any means, in fact I used it even before becoming a curl fan. I've used it to d/l several entire sites for archival study, it's utility should not be minimized. I made a 'cheat sheet' of all the c/l options so I could use it to it's full advantage, Lots of Options! So, that said, personal choice comes to mind, we're very lucky to have the all the Options that we do! ;^)

Personally I write all this class of net-code these days in Ruby.

2 Likes

:). There are a lot easier ways to have a robust HTTP client in 2022 than writing from scratch in C, that is for sure.

2 Likes

Haven't gotten into Ruby very much beyond knowing it exists, and keeping an interpreter installed for the occasional stray script that 'automagically' shows up at just the right time?
That said, it took me forever to warm up to Python, and I still tend to think of it as a sort of 'rapid prototyping' tool to test ideas to later implement in C/C++, but that's just Me! ;^)

This topic was automatically closed after 2 days. New replies are no longer allowed.