Reputation: 56
I'm currently trying to code an HTTP post request to be able to register on a website from my program. When I run it I get no errors and even the CURLOPT_VERBOSE says that everything went successful.
I hope you guys can give me an advise or help, thanks in advance!
Verbose output:
* Trying 151.139.128.8:443...
* Connected to fontawesome.com (151.139.128.8) port 443 (#0)
> POST /api/account HTTP/1.1
Host: fontawesome.com
User-Agent: M1000/1.0
Accept: */*
Content-Type: application/vnd.api+json; charset=UTF-8
Content-Length: 103
* upload completely sent off: 103 out of 103 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 415 Unsupported Media Type
< Date: Fri, 07 Aug 2020 12:45:12 GMT
< Cache-Control: max-age=0, private, must-revalidate
< cross-origin-window-policy: deny
< Server: Cowboy
< Strict-Transport-Security: max-age=31536000; preload; includeSubDomains
< X-Content-Type-Options: nosniff
< x-download-options: noopen
< X-Frame-Options: SAMEORIGIN
< x-permitted-cross-domain-policies: none
< x-request-id: Fij9D99y9W_KuwABOosh
< X-XSS-Protection: 1; mode=block
< X-HW: 1596804312.cds067.am5.hn,1596804312.cds153.am5.sc,1596804312.cds153.am5.p
< Connection: keep-alive
< Content-Length: 0
<
* Connection #0 to host fontawesome.com left intact
OK!
My code :
curl_global_init(CURL_GLOBAL_ALL);
CURL* curl = curl_easy_init();
CURLcode res;
if (curl) {
const char* c = "https://fontawesome.com/api/account";
curl_easy_setopt(curl, CURLOPT_URL, c);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "M1000/1.0");
const char *data = "{\"meta\":{},\"data\":{\"type\":\"account\",\"attributes\":{\"email\":\"[email protected]\",\"is - free\":true}}}";
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(data));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
res = curl_easy_perform(curl);
if (res != 0) {
curl_easy_cleanup(curl);
curl_global_cleanup();
F.SetError__(res);
return;
}
else
{
F.Print("OK!");
}
}
curl_easy_cleanup(curl);
curl_global_cleanup();
Upvotes: 0
Views: 1220
Reputation: 56
Thanks to KiNgMaR I fixed it.
I started to learn a bit more about headers and HTTP requests and finally found the problem. I was sending the whole time an unsupported media type for the server. After some research I found out wich Content-Type the server accepts, it was "Content-Type: application/vnd.api+json"
.
What I added to my code :
list = curl_slist_append(list, "Content-Type: application/vnd.api+json");
list = curl_slist_append(list, "Accept: application/vnd.api+json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
Upvotes: 0
Reputation: 1567
Please notice this line in the response:
< HTTP/1.1 415 Unsupported Media Type
You probably want to use curl_easy_getinfo
with CURLINFO_RESPONSE_CODE
to retrieve and check the HTTP status code from the response.
Depending on the API, response 415 could have a different meaning. In this case it's likely that the server expects a Content-Type: application/json
header in the request.
Upvotes: 1