Header Parametter in HttpRequest

I am adding header in httprequest but on server none of the parameter received. please guide me if i am doing mistake in adding header.my current cocos2dx version is 3.4

headers.push_back("Content-Type: application/json; charset=utf-8");
headers.push_back("Accept: application/json");
headers.push_back("access-token:yqkOb2VzshqSxUl6Ou2iJQ");
headers.push_back("client:zCleOHRMG3Ge1ZYu3Is51Q");
headers.push_back("uid:shauket.gvs@hgaa");

Please provide your complete code.

Did you call setHeaders?

Yes i did.

request->setHeaders(headers);

@cocos2dx please reply on this post.

Get rid of the space after the colon.

Did you set the header type to POST?

It seems headers are only supported with POST/PUT:

@iQD thanks for reply. i removed spaces but result is same,yes i set request type to delete, because i am deleting data. please clarify header type or request type ?

Ah sorry, my bad. I meant request type.
The implementation requires the request type to be POST or PUT for header support:

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

but server required Delete type from client side.I used put and post in client side to check the behaviour but same response from server side. no header data received in server side

I can only tell for 3.8.
You would need to dig into the source and check, if HttpRequest supports delete.

Did you check what really was sent out? E.g. with Wireshark.

If that does not gives any hints, please provide your full code.

in 3.8 Delete type is supported ?

As I can see, it’s implemented in the enum and as it’s using curl, it should not be a problem.
IIRC 3.9 dropped curl and is using the native functions.

/**
 * The HttpRequest type enum used in the HttpRequest::setRequestType.
 */
enum class Type
{
    GET,
    POST,
    PUT,
    DELETE,
    UNKNOWN,
};
1 Like

@iQD same enum are available for 3.4 but header param not attaching in request call.I don’t know what mistake i am doing.I want to know if headers are working in 3.8 then i can take chance to move from 3.4 to 3.8

this is my request call.kindly go through and let me know if i am doing mistake in adding headers.

cocos2d::network::HttpRequest* request = new cocos2d::network::HttpRequest();
if (headerparam != nullptr) {
request->setRequestType(cocos2d::network::HttpRequest::Type::DELETE);
}
else
{
request->setRequestType(cocos2d::network::HttpRequest::Type::POST);
}

request->setUrl(url);
request->setTag(type);

std::vector<std::string> headers;


if (params != nullptr) {
    request->setRequestData(params, strlen(params));
}
headers.push_back("Content-Type:application/json; charset=utf-8");
headers.push_back("Accept:application/json");


if (headerparam != nullptr) {
    
    cocos2d::Value headerval = ParseUtils::convertDocumentToCObject(headerparam);
    if (headerval.isNull() ) {
        
        MessageBox("error in parsing data", "abc");
    }
    else
    {
        headers.push_back("access-token:yqkOb2VzshqSxUl6Ou2iJQ");
        headers.push_back("client:yqkOb2VzshqSxUl6Ou2iJQ");
        headers.push_back("uid:gegegrgerhgerh");
                    
    }
    
}


request->setHeaders(headers);

request->setResponseCallback( [=] (network::HttpClient* client, network::HttpResponse* response) {
    if (!response)
    {
        CCLOG("response not found");
        return;
    }
    if (response && response->getResponseCode() == 200 && response->getResponseData()) {
        vector<char> *data = response->getResponseData();
        if (header) {
            
            vector<char> *headerdata = response->getResponseHeader();
            this->parseResponse(data,headerdata,request->getTag());
        }
        else
        {
            this->parseResponse(data,nullptr,request->getTag());

        }
        
    }
    else {
        CCLOG("%s", ("Error " + to_string(response->getResponseCode()) + " in request").c_str()); //error
        const char *error = __String::createWithFormat("Error %ld in request",response->getResponseCode())->getCString();
        this->delegate->serverError(error);

    }
});


cocos2d::network::HttpClient::getInstance()->send(request);
request->release();

I did not find any issues in your code.
Test my code and respond.

For testing, I dropped the missing stuff from your code, as you are using some variables, you did not provide code for.
Works perfect for me with 3.8:

// http test
cocos2d::network::HttpRequest* request = new cocos2d::network::HttpRequest();
request->setRequestType(cocos2d::network::HttpRequest::Type::DELETE);

request->setUrl("http://127.0.0.1:8080/");

std::vector<std::string> headers;
headers.push_back("Content-Type:application/json; charset=utf-8");
headers.push_back("Accept:application/json");

request->setHeaders(headers);

request->setResponseCallback([=](network::HttpClient* client, network::HttpResponse* response) {
	if (!response)
	{
		CCLOG("response not found");
		return;
	}
	if (response && response->getResponseCode() == 200 && response->getResponseData()) {
		std::vector<char> *data = response->getResponseData();
	}
	else {
		CCLOG("%s", ("Error " + std::to_string(response->getResponseCode()) + " in request").c_str()); //error
		const char *error = __String::createWithFormat("Error %ld in request", response->getResponseCode())->getCString();
	}
});


cocos2d::network::HttpClient::getInstance()->send(request);
request->release();

I just used this echo server here:

Server echo:

C:\Windows\system32>f:\reflect.py
Listening on localhost:8080

----- Request Start ----->

/
Host: 127.0.0.1:8080
Accept-Encoding: deflate, gzip
Content-Type: application/json; charset=utf-8
Accept: application/json

<----- Request End -----

Thanks @iQD but problem is my server is written in rubby on rail, i used same parameter that you mention above but i get in response nothing from server side, server guy told me he did not received any parameter form client side in header.

Try using python or php to verify. If you can get the headers via python or php, then the problem is not with your frontend code. Your server guy probably needs to check his side.

same parameter i set in advanced rest client to verify delete call. it works on rest client.its mean no issue is with server side.One thing i see in HttpClient-ios.mm class he did not handling Delete request to add parameter in header file. can you guys tell me cocos2dx guy so i contact them directly via email. your help will be appreciated.thanks in advance.

Thanks guys special thanks to you @iQD @duracellrabbid i resolved this issue.I made changes in HttpClient-ios.mm file.added Delete parameter

if([requestType isEqual: @“POST”] || [requestType isEqual: @“PUT”] || [requestType isEqual: @“DELETE”])
{
if ([requestType isEqual: @“PUT”] || [requestType isEqual: @“DELETE”])
{
[nsrequest setValue: @“application/x-www-form-urlencoded” forHTTPHeaderField: @“Content-Type”];
}
/* get custom header data (if set) /
std::vectorstd::string headers=request->getHeaders();
if(!headers.empty())
{
/
append custom headers one by one */
for (std::vectorstd::string::iterator it = headers.begin(); it != headers.end(); ++it)
{
unsigned long i = it->find(’:’, 0);
unsigned long length = it->size();
std::string field = it->substr(0, i);
std::string value = it->substr(i+1, length-i);
NSString *headerField = [NSString stringWithUTF8String:field.c_str()];
NSString *headerValue = [NSString stringWithUTF8String:value.c_str()];
[nsrequest setValue:headerValue forHTTPHeaderField:headerField];
NSLog(@“header value and key %@:%@”,headerField,[nsrequest valueForHTTPHeaderField:headerField]);
}
}

What are you trying to do?

We have video tutorials covering this