使用libcurl更改内容类型 [英] Change Content-type using libcurl

查看:126
本文介绍了使用libcurl更改内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

#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

因此它返回415不支持的媒体类型错误代码.

So it returns 415 Unsupported Media Type error code.

我在StackOverflow上发现它可能是由Context-type中的charset = UTF-8引起的.所以我的问题是,如何编辑该字段以满足我的需要(删除charset = UTF-8).链接

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

如何更改该内容类型.由于我是HTTP协议的新手,请详细说明.

How to change that Content-type. Since I'm new to HTTP protocol, please elaborate.

推荐答案

根据URL /rest-v1/devices/did1/tasks ,您正在使用 IBM Business Process Manager REST界面.该文档指出:

According to the URL /rest-v1/devices/did1/tasks you are using the IBM Business Process Manager REST Interface. The documentation states:

媒体类型

请求或响应中包含的数据将是以下之一以下媒体类型:

The data included in requests or responses will be of one of the following media types:

应用程序/json:JSON(JavaScript对象表示法)-这是默认的响应内容类型.有关每个返回对象的详细格式,请参见每个操作的JSON模式规范.

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.

应用程序/xml :XML(可扩展标记语言)-基于XML的数据格式由产品随附的XML模式指定./properties/schemas/bpmrest/v1目录.摘录文档中还为每个架构提供了这些架构操作.

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.

应用程序/x-javascript :JSONP(带填充的JSON)-此格式可以用作JSON的替代格式.在这种情况下,每个返回的JSON响应都是包装在JavaScript回调函数调用中.要使用这个功能,还必须指定回调URI查询参数.

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.

因此,您将必须为您的请求选择这些内容类型之一(而不是上下文类型!),而不是 application/x-www-form-urlencoded .在示例代码段中,您使用JSON,因此此处的适当数据类型为 application/json .您可以将其设置如下:

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);

这应该消除您观察到的HTTP错误415.

This should eliminate the HTTP error 415 you observe.

这篇关于使用libcurl更改内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆