#c #json #libcurl
#c #json #libcurl
Вопрос:
Я пытаюсь понять, как отправлять данные json в веб-службу. json.phph y просто php, который печатает все $ _REQUEST, отправляемые в скрипт. но в итоге я ничего не получаю, я попытался отправить форму в формате urlenc и просто обычные данные D: у меня заканчиваются идеи, как это можно сделать.
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
int main(int argc, char *argv[])
{
CURL *curl;
CURLcode res;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Content-Type: application/json";
curl_global_init(CURL_GLOBAL_ALL);
/*
curl_formadd(amp;formpost,
amp;lastptr,
CURLFORM_COPYNAME, "json",
CURLFORM_COPYCONTENTS, "reactantsJSON={"O=O":{"N":1}}amp;productsJSON= ["O=O","[O]"]amp;temperature=2273.15amp;pressure=101.325",
CURLFORM_END);
*/
curl = curl_easy_init();
/* initalize custom header list (stating that Expect: 100-continue is not
* wanted */
headerlist = curl_slist_append(headerlist, buf);
if(curl) {
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1/json.php");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl,CURLOPT_POST,1);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS, "reactantsJSON={"O=O":{"N":1}}&productsJSON=["O=O","[O]"]&temperature=2273.15&pressure=101.325" );
// curl_easy_setopt(curl,CURLOPT_POSTFIELDS, "reactantsJSON={"O=O": {"N":1}}amp;productsJSON=["O=O","[O]"]amp;temperature=2273.15amp;pressure=101.325" );
// if ( (argc == 2) amp;amp; (!strcmp(argv[1], "noexpectheader")) )
/* only disable 100-continue header if explicitly requested */
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
/* free slist */
curl_slist_free_all (headerlist);
}
return 0;
}
Ответ №1:
Вам нужны CURLOPT_HTTPPOST или CURLOPT_POSTFIELDS, а не оба. Они будут выдавать два разных вида POST-запросов.