How to POST data?

Sockets, HTTP, SSL, Ad Hoc, NP, Bluetooth, Wifi, etc.
Post Reply
MyLegGuy
Posts: 2
Joined: Sun Jan 08, 2017 2:13 am

How to POST data?

Post by MyLegGuy » Sat Feb 11, 2017 6:01 am

Hi, I'm trying to send some data with POST to a webpage.
I've been working off the network sample.
I haven't changed the code that much. Here's the code I think is the most relevant:

Code: Select all

char* url = "http://192.168.1.234/test.php";
char* postData="test=happyman";
int tpl = sceHttpCreateTemplate("PS Vita Sample App", 1, 1);
int conn = sceHttpCreateConnectionWithURL(tpl, url, 0);
int request = sceHttpCreateRequestWithURL(conn, SCE_HTTP_METHOD_POST, url, strlen(postData));
int handle = sceHttpSendRequest(request, postData, strlen(postData));
Here's all of the code.

The I want "test" to be "happyman" in the POST data.
Here's what I have for test.php:

Code: Select all

<?php
echo "echoz.";
file_put_contents("file.txt", $_POST["test"]);
?>
I always get "Undefined index: test".

One of the things I'm most confused about is the second argument of sceHttpSendRequest.
It's the argument for the POST data, but it accepts any type of pointer. I wasn't sure what to do, so I just passed a string (named postData).

The final goal is to have test.php write "happyman" in file.txt on my server.
My question is, what is wrong with my code?

noname120
Posts: 17
Joined: Sun Dec 11, 2016 7:39 pm

Re: How to POST data?

Post by noname120 » Sat Feb 11, 2017 7:20 am

Could you change the url to a RequestBin to see exactly what you receive?

User avatar
DevDavisNunez
Posts: 7
Joined: Sun Jan 01, 2017 3:19 am

Re: How to POST data?

Post by DevDavisNunez » Sat Feb 11, 2017 2:24 pm

Hello..
It is necessary to add a header so that the server actually recognizes that it is an http POST request.

Try:

Code: Select all

int request = sceHttpCreateRequestWithURL(conn, SCE_HTTP_METHOD_POST, url, strlen(postData));
sceHttpAddRequestHeader(request, "Content-Type", "application/x-www-form-urlencoded", 0); // Extra header for send type of post
int handle = sceHttpSendRequest(request, postData, strlen(postData));
strlen only fail if it uses to send binary data, but for simple text work. :D
I hope I have been helpful.

MyLegGuy
Posts: 2
Joined: Sun Jan 08, 2017 2:13 am

Re: How to POST data?

Post by MyLegGuy » Sat Feb 11, 2017 6:03 pm

noname120 wrote:
Sat Feb 11, 2017 7:20 am
Could you change the url to a RequestBin to see exactly what you receive?
DevDavisNunez wrote:
Sat Feb 11, 2017 2:24 pm
Hello..
It is necessary to add a header so that the server actually recognizes that it is an http POST request.

Try:

Code: Select all

int request = sceHttpCreateRequestWithURL(conn, SCE_HTTP_METHOD_POST, url, strlen(postData));
sceHttpAddRequestHeader(request, "Content-Type", "application/x-www-form-urlencoded", 0); // Extra header for send type of post
int handle = sceHttpSendRequest(request, postData, strlen(postData));
strlen only fail if it uses to send binary data, but for simple text work. :D
I hope I have been helpful.
Thanks, adding a header worked!
And thanks for telling me about RequestBin, I'm sure it'll be useful the next time I have a problem.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest