POST Method using cURL

Hello,

I’m trying to implement a POST method with parameters using cURL but I don’t know how to start. Can anyone give me pointers?

Thanks.

http://www.jesusbosch.com/2012/08/internet-communications-with-cocos2d-x.html

hope it helps

https://github.com/cocos2d/cocos2d-x/tree/gles20/extensions/network
And the sample code is here https://github.com/cocos2d/cocos2d-x/tree/gles20/samples/TestCpp/Classes/ExtensionsTest/NetworkTest

It works. But it maybe deprecated after 5~6 months if we have better design to cover both native + hybrid + html5 versions.

Hi Walzer,

You mean that cURL will be deprecated, or the network extension?

Cheers,

JB

Thanks for the links.

I already used cURL to parse XML data and it all worked fine, but what I want to do is create POST methods, something the links seem to not talk about.

I found this:

And will attempt to convert it to C++.

Also, since I’ve already got your attention, can you help me in something else:

I want to to be able to put an image in the read buffer, then use this buffer to put on a cctexture then to a ccsprite. Evertyhing is fine except for the writefunction. Here is the thread in question (note that this is not as important as the POST method because I could just download the image to the resources directory and use it from there (right?)).

Here is the previous thread.
http://www.cocos2d-x.org/boards/6/topics/12507?r=12578#message-12578

Jesus Bosch wrote:

Hi Walzer,
>
You mean that cURL will be deprecated, or the network extension?
>
>
Cheers,
>
JB

No, cURL will always there. I mean CCHttpClient, my wrapper maybe deprecated in future.

Walzer,

I tried your code but I’m getting these two errors:

ExtensionMacros.h file not found

and

cocos-ext.h file not found

I downloaded the Cocos2d-x Extensions folder and cloned it into my project but still to no avail.

Nevermind, found em. Copying them now.

I need some help, Walzer.

I copied the folder but I keep getting ‘X not found’ at the includes. For example

#include "a/b/c/file.h" <- NOT FOUND

But if I remove the dashes to:

#include "file.h <- NO ERROR

This worked until I reached

../GUI/CCControlExtension/CCInvocation.h

If I remove the dashes I get errors at:

SEL_CCControlHandler

and at some other definitions and macros.

I got it to run and test, god that was awful.

My version of the engine was very outdated and I had to download the new one. I’m still having issues with some other extentions (the new edit box) but the POST method works now.

Thanks for nothing.

thanks for nothing? I guess you should show more respect to other forum users… and specially Walzer, that is one of the guys that has created Cocos2d-x… if you are so unhappy you might use other engines.

I already thanked him for the original help, but when I was stumbling afterwards there was no answer.

I know he’s one of the creators, which means he probably know the answer I’m looking for, but doesn’t want to help.

and it is mandatory for him to answer in less than 24 or 48hrs? do you pay him? nevermind… I’ll not answer any of your questions if I see you in this forum again.

Bye

I posted the question in the link two months ago.

Anyway sorry for offending.

Later.

If anyone’s wondering how to do it.

Import

#include "HttpClient.h"
#include "HttpRequest.h"
#include "HttpResponse.h"

Then use this (taken from the sample/test):

        cocos2d::extension::CCHttpRequest* request = new cocos2d::extension::CCHttpRequest();
        request->setUrl("URL");
        request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpPost);
        request->setResponseCallback(this, callfuncND_selector(HelloWorld::onHttpRequestCompleted));

        char name[50];

        sprintf(name, "username=%s&password=%s", l->getString(), l2->getString());

        const char* postData = name;
        request->setRequestData(postData, strlen(postData)); 

        request->setTag("POST test");
        CCLog(postData);
        cocos2d::extension::CCHttpClient::getInstance()->send(request);
        request->release();

void HelloWorld::onHttpRequestCompleted(cocos2d::CCNode *sender, void *data)
{
    cocos2d::extension::CCHttpResponse *response = (cocos2d::extension::CCHttpResponse*)data;

    if (!response)
    {
        return;
    }

    // You can get original request type from: response->request->reqType
    if (0 != strlen(response->getHttpRequest()->getTag())) 
    {
        CCLog("%s completed", response->getHttpRequest()->getTag());
    }

    int statusCode = response->getResponseCode();
    char statusString[64] = {};
    sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
    CCLog("response code: %d", statusCode);

    if (!response->isSucceed()) 
    {
        CCLog("response failed");
        CCLog("error buffer: %s", response->getErrorBuffer());
        return;
    }

    // store data
    std::vector *buffer = response->getResponseData();
    char* concatenated = (char*) malloc(buffer->size() + 1);
    std::string s2(buffer->begin(), buffer->end());

    strcpy(concatenated, s2.c_str());

    CCLog("===");
    CCLog(concatenated);

}

the part of ‘store data’ was very useful to me, I don’t know the existence of the malloc method

thank u thank u thank u very much,it’s very helpful to me about the part of “store data”.Finaly,i found out how to use CCHttpResponse and get the json data of use jsoncpp.

Same here, I am using this with jsoncpp! :slight_smile:

how to send mydata much time at once serUrl???
Husam Aldahiyat wrote:

If anyone’s wondering how to do it.
>
Import
>
[…]
>
Then use this (taken from the sample/test):
>
[…]
>
[…]

hi, i have follow above steps but it’s give error complie time…
libExtensions.lib(HttpClient.obj) : error LNK2019: unresolved external symbol impcurl_easy_setopt referenced in function “bool __cdecl cocos2d::extension::configureCURL(void *)” (?configureCURL@extension@cocos2d@YA_NPAXZ)

pls. give me advice… what is mistake or error.