Reputation: 99
I have this code:
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "localhost/rest-v1/devices/did1/tasks");
curl_easy_setopt(curl,CURLOPT_PORT,22080);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"hello\" : \"darkness\"}");
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
return 0;
}
When I run it I get this print on console:
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 22080 (#0)
> POST /rest-v1/devices/did1/tasks HTTP/1.1
Host: localhost:22080
Accept: */*
Content-Length: 16
Content-Type: application/x-www-form-urlencoded
* upload completely sent off: 16 out of 16 bytes
< HTTP/1.1 415 Unsupported Media Type
< Server: Restlet-Framework/2.3.9
< Date: Fri, 23 Jun 2017 06:44:09 GMT
< Content-type: application/json; charset=UTF-8
< Content-language: *
< Content-length: 35
< Accept-ranges: bytes
<
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
So it returns 415 Unsupported Media Type error code.
I found on StackOverflow that it may be caused by charset=UTF-8 in Context-type. So my question is, how can I edit this field to meet my needs (remove charset=UTF-8). link
How to change that Content-type. Since I'm new to HTTP protocol, please elaborate.
Upvotes: 7
Views: 12818
Reputation: 18410
According to the URL /rest-v1/devices/did1/tasks
you are using the IBM Business Process Manager REST Interface. The documentation states:
Media Types
The data included in requests or responses will be of one of the following media types:
application/json: JSON (JavaScript Object Notation) - This is the default response content type. For the detailed format of each returned object, see the JSON schema specifications for each operation.
application/xml: XML (eXtensible Markup Language) - The format of XML-based data is specfied by the XML schemas supplied with the product in the /properties/schemas/bpmrest/v1 directory. Excerpts of these schemas are also provided in the documentation for each operation.
application/x-javascript: JSONP (JSON with Padding) - This format can be used as an alternative to JSON. In this case, each returned JSON response is wrapped in a JavaScript callback function invocation. To use this feature, you must also specify the callback URI query parameter.
So you will have to choose one of these Content-Types (not Context-Type!) for your request instead of application/x-www-form-urlencoded
. In your sample code snippet, you use JSON, so the appropriate data type is application/json
here. You can set it as follows:
struct curl_slist *hs=NULL;
hs = curl_slist_append(hs, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs);
This should eliminate the HTTP error 415 you observe.
Upvotes: 13