Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Beginner webapi Help
Hi everyone!
 

Few days ago I decided to use a WebAPI to make a little program. (Steam WebAPI to be exact)
 
Now, I know you send some sort of URL (request) and it returns an answer in JSON format. (Or xml or vdf in this instance)
 
I know how to edit this information (I believe I can learn :p) But I have no idea how to request them in C language. (to send a URL and wait for answer)
 
I got a suggestion to use Python to mess with all this but I'm already kinda familiar with C/C++ so I want to use it. I found this; http://uriparser.sourceforge.net/ [^]. Am I on the right path? Can I fetch *text* from links like this using these Libraries? *http://api.steampowered.com/IEconItems_440/GetSchema/v0001/?key=6D52161D23855A4E3319B63ED165272E[^]
 
Or is there any way else to do this in C?
 
Also, let's say I did somehow manage to use the WebApp to get some info. After that how would I send datas? (Like, add a friend or invite a player to a group)
 

Please point me in the right direction. Thanks a lot in advance!
Posted 28-Oct-12 8:05am
cbt13311
Comments
enhzflep at 28-Oct-12 13:41pm
   
tl;dr - Basically speaking, you just need to put the right info in the header of the request. The server looks at it then decides what to return to you.
Basically, you want to start out with the ability to download a file from a URL. You may then go on to add extra info to the request header, or look into sending a POST request.
 
There's lots of 'how to download a file in c' type articles here on CP. Here's some code I threw together some time ago for a RSS reader. There's lots of things that could be done better, but the intention was to make the code clear and readable.
 
Download file in C

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

HTTP is a simple protocol. You make a tcp connection and send a request then wait for the response. You repeat this over and over. In case of HTTP 1.0 you always make new connections for each request while in HTTP 1.1 you can send and receive multiple requests over the same connection (you can even use pipelining but some HTTP clients and severs don't handle that well).
 
Both a request and a response contains the following things:
- HTTP protocol in use (1.0 or 1.1)
- Request parameters (like url) in case of a request or response status (HTTP status code) in case of a response
- Headers: these are string key/value pairs that contain only ASCII characters (!!!).
- Optional payload (it can be zero bytes or even arbitrary binary data)
 
Some important notes:
- I don't recommend using unicode strings in the header (because its ASCII according to the standard) however its possible, when both the client and server side is under my control I convert the strings to utf8 and then I url-encode the strings or base64 encode before sending. If the server isn't yours then you don't know how the server handles non-ascii characters or url encoding.
- The requested url and the header part of the request has a size limit in most servers (configurable and different in each server) so don't try to send big data in header key-value pairs or encoded as query string parameters in your url! Only the payload has unlimited size! With POST method you can send key-value parameters in the payload.
 
If you want to go the C/C++ route then use libcurl as your HTTP client to perform your requests to the server: http://curl.haxx.se/libcurl/c/libcurl-tutorial.html[^]
You can find libraries for a lot of languages for JSON on its website: http://www.json.org/[^]
 
Independently form your project I highly recommend learning python. Not only because dealing with http is easy in it but because today its one of the best and simplest scripting languages with really good library support and its quite platform independent. I always prototype HTTP server and client behavior in this language. You can use it to implement HTTP server side behavior as cgi or wsgi (by extending well known servers like Apache). If you use only a subset of python (RPython) then you can convert it to binary executables using PyPy.
There should be at least one scripting language in your toolbox and python is a really good choice.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 28 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100