Estou tentando enviar os dados tanto na URL quanto no corpo usando o PUT a ideia era fazer com que ele funcionasse exatamente como o método POST, porém sendo o método PUT.
Isso porque vejo muitas requisições onde a url seria por exemplo /book/ e no corpo teria um JSON (com os dados usados para atualizar o elemento daquele id).
Estou tentando fazer isso usando a libcurl em C++, porém apesar do REQUEST_METHOD vir PUT eu não recebo os dados. A implementação do POST, GET e DELETE enviam os dados. Estou tendo problemas exclusivamente com o PUT a principio.
// Código em C++
I9CorpResponse * I9CorpRequest::request(int method, const char *url, const char *body, I9CorpRequestOptions * options) {
I9CorpResponse * response = nullptr;
long statusCode;
CURLcode res;
CURL *curl;
char mBufferHeader[I9CORP_BUFFER_MESSAGE];
char mTmp[I9CORP_BUFFER_MESSAGE];
std::map<std::string, char *> mHeaders;
struct curl_slist *chunk = nullptr;
char *mBufferParam = nullptr;
std::string content;
curl = curl_easy_init();
if (curl == NULL) {
return nullptr;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
if (options != nullptr) {
std::map<std::string, char *>::iterator it;
mHeaders = options->getHeaders();
it = mHeaders.begin();
for (it = mHeaders.begin(); it != mHeaders.end(); ++it) {
snprintf(mBufferHeader, I9CORP_BUFFER_MESSAGE, "%s: %s\0"
, it->first.c_str()
, it->second);
chunk = curl_slist_append(chunk, mBufferHeader);
}
if (chunk != nullptr) {
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
}
if (body == nullptr) {
mBufferParam = options->createQueryParams();
}
}
switch (method) {
case METHOD_GET:
snprintf(mTmp, I9CORP_BUFFER_MESSAGE, "%s?%s\0", url, mBufferParam);
curl_easy_setopt(curl, CURLOPT_URL, mTmp);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
break;
case METHOD_POST:
curl_easy_setopt(curl, CURLOPT_POST, 1L);
if (body != nullptr) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
} else if (mBufferParam != nullptr) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, mBufferParam);
}
break;
case METHOD_PUT:
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
if (body != nullptr) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
} else if (mBufferParam != nullptr) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, mBufferParam);
}
break;
case METHOD_DELETE:
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
snprintf(mTmp, I9CORP_BUFFER_MESSAGE, "%s?%s\0", url, mBufferParam);
curl_easy_setopt(curl, CURLOPT_URL, mTmp);
break;
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, I9CorpRequest::callbackDataWrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
if (mBufferParam != nullptr) {
free(mBufferParam);
}
/* Check for errors */
if (res == CURLE_HTTP_RETURNED_ERROR) {
response = new I9CorpResponse();
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &statusCode);
response->setStatusCode(statusCode);
} else if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
} else {
response = new I9CorpResponse();
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &statusCode);
response->setStatusCode(statusCode);
response->setContent(content.c_str());
}
/* free the custom headers */
if (chunk != nullptr) {
curl_slist_free_all(chunk);
}
/* always cleanup */
curl_easy_cleanup(curl);
return response;
}
Nos exemplos que estou fazendo o body é nullptr e o mBufferParam tem conteúdo
"lastname=Brito&name=Sileno&username=test"
Para testar eu escrevi o seguinte código em PHP, mas não consigo ler os valores, pois não chegam no php.
<?php
var_export(
array(
'server'=> $_SERVER,
'get'=> $_GET,
'post'=> $_POST,
'request'=> $_REQUEST
)
);