[SOLVED] HttpRequest Error for POST

I have a request that uses params that look like this:
string params = “product=aprod&name=product_name”;

HttpRequest* request = new HttpRequest();
string url = string(“https://www.mydomain.com/API//RecordIt”);
request->setUrl(url.c_str());
request->setRequestType(HttpRequest::Type::POST);
request->setRequestData(data.c_str(), strlen(data.c_str()));

The server returns an error saying that “product” is empty.

If I send it as a GET it works perfectly.

HttpRequest* request = new HttpRequest();
string url = string(“https://www.mydomain.com/API//RecordIt?product=aprod&name=product_name”);
request->setUrl(url.c_str());
request->setRequestType(HttpRequest::Type::GET);

It seems that the request data is not getting filled out by setRequestData. Has anyone seen this problem?

I’m running this in the latest version on iOS iPad Mini 2.

Okay, using asp.net on the server side I was able to confirm that the HttpRequest is being sent as a GET even though I have it like this:

request->setRequestType(HttpRequest::Type::POST);

How can I ensure this is sent as a POST?

Hello,
make sure from server end API is configured to receive data in request body not in request header. Also test if using headers works for you, try this one for starter.

std::vector<std::string> headers;
headers.push_back("Content-Type: application/x-www-form-urlencoded");
request->setHeaders(headers);

Hope this helps
Thanks

Hello,
make sure from server end API is configured to receive data in request body not in request header. Also test if using headers works for you, try this one for starter.

std::vector<std::string> headers;
headers.push_back("Content-Type: application/x-www-form-urlencoded");
request->setHeaders(headers);

Hope this helps
Thanks

Setting the Content-Type doesn’t help; application/x-www-form-urlencoded it is most likely the default setting. If I check the HttpRequest before it is sent by the HttpClient it says it is a POST. But, on my server I can check the HttpRequest type using a method, Request.HttpMethod which shows me that it is a GET.

ASP.NET will check the body automatically if it is a POST. I also checked the body directly and there was no data there. If I use GET then everything works as expected.

I think the issue may be that the HttpClient is not sending it as POST even though it is set that way. I can’t drill down to that so I assume it is in a pre-compiled library.

I noted that the comments for HttpClient::getInstance()->send specifically says it will do a GET.

/**
 * Add a get request to task queue
 *
 * @param request a HttpRequest object, which includes url, response callback etc.
                  please make sure request->_requestData is clear before calling "send" here.
 */

I do not see any seperate method for doing a post. Also, it says to use this method in the docs:
http://www.cocos2d-x.org/wiki/How_to_use_CCHttpClient

Maybe this has been changed recently?

Here is information from the v3.4 docs on HttpClient::send :

Add a get request to task queue. 
Parameters
  
    requesta HttpRequest
 object, which includes url, response callback etc. please make sure 
request->_requestData is clear before calling "send" here. 

Note that is says to “clear” the _requestData before sending. This seems to be saying that you can only use this to do a GET.

Can someone confirm that the HttpClient can only do a GET request?

@Lazy_Gamer I think you might be right that is something on my server, which is odd because I have been using asp.net for years and have never encountered this issue before.

When I look at the response in cocos2d-x using:

auto t = response->getHttpRequest()->getRequestType();

I see that t is equal to HttpRequest::Type::POST. So, if it is returned as a POST then there may be something wrong with my server.

I posted my data to https://httpbin.org/post and it returns the correct data in the forms area. So, that tells me for sure that it is in the code of my website. I am not sure why, since if I send the data using jQuery.post it works fine on my website.

Either way, the problem does not seem to be cocos2d-x since it is sending to httpbin.org correctly.

Okay, this took a lot of digging but the answer turns out to be quite simple! The cause of the POST turning into a GET is a 301 redirect (which I found in my server logs). That is because my URL looks like this:

request->setUrl("https://www.mydomain.com/API/RecordIt");

which any good web guy (except me, apparently) knows doesn’t end in slash, so you are redirected by the server to a URL with a slash on the end. By changing my request to this:

request->setUrl("https://www.mydomain.com/API/RecordIt/");

It now is received by my website as a POST.

The funny part is that when the URL is redirected it defaults to a GET and the data from the POST is discarded. You receive the request with an empty form. Odd.

1 Like

:slight_smile: Good finding, This was the scenario with me a while back when i tried to create a unified API for GET/POST, apparently i ended up doing things separately.