#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <inttypes.h>
Result http_post(
const char* url,
const char* data)
{
char *newurl=NULL;
u32 contentsize=0, readsize=0, size=0;
printf("POSTing %s\n", url);
do {
printf("return from httpcOpenContext: %" PRIx32 "\n",ret);
printf("return from httpcSetSSLOpt: %" PRIx32 "\n",ret);
printf("return from httpcSetKeepAlive: %" PRIx32 "\n",ret);
printf("return from httpcAddRequestHeaderField: %" PRIx32 "\n",ret);
printf("return from httpcAddRequestHeaderField: %" PRIx32 "\n",ret);
printf("return from httpcAddPostDataRaw: %" PRIx32 "\n",ret);
if(ret!=0){
if(newurl!=NULL) free(newurl);
return ret;
}
if(ret!=0){
if(newurl!=NULL) free(newurl);
return ret;
}
if ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308)) {
if(newurl==NULL) newurl = malloc(0x1000);
if (newurl==NULL){
return -1;
}
url = newurl;
printf("redirecting to url: %s\n",url);
}
} while ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308));
if(statuscode!=200){
printf("URL returned status: %" PRIx32 "\n", statuscode);
if(newurl!=NULL) free(newurl);
return -2;
}
if(ret!=0){
if(newurl!=NULL) free(newurl);
return ret;
}
printf("reported size: %" PRIx32 "\n",contentsize);
buf = (
u8*)malloc(0x1000);
if(buf==NULL){
if(newurl!=NULL) free(newurl);
return -1;
}
do {
size += readsize;
lastbuf = buf;
buf = realloc(buf, size + 0x1000);
if(buf==NULL){
free(lastbuf);
if(newurl!=NULL) free(newurl);
return -1;
}
}
if(ret!=0){
if(newurl!=NULL) free(newurl);
free(buf);
return -1;
}
lastbuf = buf;
buf = realloc(buf, size);
if(buf==NULL){
free(lastbuf);
if(newurl!=NULL) free(newurl);
return -1;
}
printf("response size: %" PRIx32 "\n",size);
printf((char*)buf);
printf("\n");
free(buf);
if (newurl!=NULL) free(newurl);
return 0;
}
int main()
{
ret=http_post("http://httpbin.org/post", "{\"foo\": \"bar\"}");
printf("return from http_post: %" PRIx32 "\n",ret);
{
break;
}
return 0;
}
bool aptMainLoop(void)
Main function which handles sleep mode and HOME/power buttons - call this at the beginning of every f...
PrintConsole * consoleInit(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
void gfxSwapBuffers(void)
Updates the configuration of both screens.
void gfxInitDefault(void)
Initializes the LCD framebuffers with default parameters This is equivalent to calling:
@ GFX_BOTTOM
Bottom screen.
Definition: gfx.h:27
void gfxExit(void)
Deinitializes and frees the LCD framebuffers.
void gfxFlushBuffers(void)
Flushes the data cache for the current framebuffers.
#define gspWaitForVBlank()
Waits for VBlank.
Definition: gspgpu.h:151
@ KEY_START
Start.
Definition: hid.h:15
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
void hidScanInput(void)
Scans HID for input data.
Result httpcGetResponseStatusCode(httpcContext *context, u32 *out)
Gets the response code of the HTTP context.
Result httpcBeginRequest(httpcContext *context)
Begins a HTTP request.
Result httpcGetResponseHeader(httpcContext *context, const char *name, char *value, u32 valuebuf_maxsize)
Gets a response header field from a HTTP context.
Result httpcGetDownloadSizeState(httpcContext *context, u32 *downloadedsize, u32 *contentsize)
Gets the download size state of a HTTP context.
Result httpcSetKeepAlive(httpcContext *context, HTTPC_KeepAlive option)
Sets Keep-Alive for the context.
Result httpcAddRequestHeaderField(httpcContext *context, const char *name, const char *value)
Adds a request header field to a HTTP context.
Result httpcInit(u32 sharedmem_size)
Initializes HTTPC. For HTTP GET the sharedmem_size can be zero. The sharedmem contains data which wil...
Result httpcDownloadData(httpcContext *context, u8 *buffer, u32 size, u32 *downloadedsize)
Downloads data from the HTTP context into a buffer.
Result httpcOpenContext(httpcContext *context, HTTPC_RequestMethod method, const char *url, u32 use_defaultproxy)
Opens a HTTP context.
void httpcExit(void)
Exits HTTPC.
Result httpcAddPostDataRaw(httpcContext *context, const u32 *data, u32 len)
Adds a POST body to a HTTP context.
#define HTTPC_RESULTCODE_DOWNLOADPENDING
Result code returned when a download is pending.
Definition: httpc.h:35
Result httpcSetSSLOpt(httpcContext *context, u32 options)
Sets SSL options for the context.
Result httpcCloseContext(httpcContext *context)
Closes a HTTP context.
HTTP context.
Definition: httpc.h:8
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
s32 Result
Function result.
Definition: types.h:42
int32_t s32
32-bit signed integer
Definition: types.h:28
uint32_t u32
32-bit unsigned integer
Definition: types.h:23