How can I post data with POST method in C programming.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How can I post data with POST method in C programming.
I extreamly want to how can we program in C code to perform post data (such as login to some website which we send the username and pass data to web server, or any type of this kind which involve POST method).
I have try to program with GET method, it's work fine. Also i search through the net all day today, but i found info that can't immediately go into the real situaion here.
Below is C Code with GET Method, you can try with
# ./httpgetconnect <IPaddress> <relative_url> => ./httpgetconnect 210.1.13.198 /
this result will print the content in html to STDOUT
Please save from this link: [HTML]http://210.1.13.198/haxpor/httpgetconnect[/HTML]
I don't know, is this for learning or a practical issue? Anyway, why not use libcurl, wget, or some other tool? If it's for learning, why not look at the source?
What I really like about opensource is the huge amount of examples - I have source code for everything
I also download curl to my computer not long ago, but I go into it search through the source but I cant found the API function that perform for Post Method in curl. Maybe I miss something but I try it so long to keep finding it.
Maybe you can show me that section of sourcecode then (if you have time)
Thank very much. It's great to be helped.
Your best bet is to read the HTTP rfc to see how to properly form a POST request.
Once you understand the protocol properly you can then translate this to C code.
I have done a bit of http/https programing and that is how I approach it.
After a long time for trying and experimenting the code with c. I decide to see the header in http which sending between my computer and server (that trap with Proxomitron 4.5 in Windows), I take that info and adapt it then try it with perl (usually very close with C), and it work with POST Method.
The sourcecode for Perl which connect to Any web server with POST Method is below.
Note
You must fill in the infomation by yourself with that bold string(url of web server, data to send, etc)
Also be careful with the line that I commented, not include '&' in calculation so you have your data plus 1 byte to fill in Content-Length: header.
_Example_
<LINK> => /member.php?act=login
<FullUrlLink> => http://www.something.org
After this perl code is work fine, then I jump it concept to C immediately but I found it wont work.
Yes, this interact with CGI script hosted on the web server.
The server return with 400 Bad Request code, still now I figuring it out what my header in C code is wrong. I do change it to be the same as Perl code above and modify it a little but the result is same it return 400 code every time.
IT MUST BE WRONG WITH HEADER, i guess that, other part is fine.
I am sure you have some account with some web server, if possible you can test this thing with the C Code above that i post
but change the line -> "GET %s .... to 'POST header' in Perl code above and run it with web server ip.
OK i will make it more comfortable for you all who trying to help.
You can download the sourcecode in .tar package (include main source and header file) for httpgetconnect, for this you can modify it to test with POST Method.
Ok everyone, i want to tell you all that the problem is the HEADER as I suggest, and it is true.
It is my mistake not to calmly and slowly see at the header. The header is wrong.
As you can see from GET Method code from above the line 'sprintf(..'
you will see "GET %s HTTP1.1.." this is wrong, the correct is "GET %s HTTP/1.1". Although that mistake work with GET Method but it wont work with POST Method.
So after all, you can try this by correct that line of header to the correct one as i tell you, then run it. (NOTE: you must fill in the info of the server and data you want to send by yourself)
Tell me if it work!
PS.
-Thanks to you all that work along and together figure it out the problem. Guttorm, slzckboy.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.