curl传输json数据

curl -H 'Content-Type: application/json' -X POST -d '{"name":"XBox","price":3999}' http://localhost:3000/api/products

在linux上可以此代码运行。

windows时,注意:

1、有两处  ‘  符号,需要改为  "  

2、Content-Type: application/json 中  :  号后不能有空格【此注意点,可能会有出入】

3、在参数中不要留空格

以下为我更改后的代码,在windows上可运行:

curl localhost:3000/api/products -X POST -d "{\"name\":\"XBox\",\"price\":3999}" -H "Content-Type:application/json" -v

这里我添加了一个  -v  ,是为了方便查看信息

有  -v  时,得到的信息:

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> POST /api/products HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.55.1
> Accept: */*
> Con
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Curl是一个非常流行的命令行工具和库,可用于传输数据,其中包括使用HTTP协议发送和接收数据。如果你想要使用Curl发送JSON数据,可以按照以下步骤操作: 1. 准备好要发送的JSON数据,将其保存为字符串或从文件中读取。例如,以下是一个JSON字符串: ``` {"name": "John Smith", "age": 30, "city": "New York"} ``` 2. 使用Curl库中的curl_easy_init()函数初始化Curl句柄。 ``` CURL *curl; curl = curl_easy_init(); ``` 3. 设置请求的URL地址。例如: ``` curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/api"); ``` 4. 设置请求的HTTP方法,这里我们使用POST方法。 ``` curl_easy_setopt(curl, CURLOPT_POST, 1L); ``` 5. 设置请求头,指定Content-Type为application/json。 ``` struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/json"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); ``` 6. 设置请求体,将JSON数据作为请求体发送。 ``` curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"name\": \"John Smith\", \"age\": 30, \"city\": \"New York\"}"); ``` 7. 执行HTTP请求,将响应保存在一个缓冲区中。 ``` CURLcode res; char *response = NULL; size_t response_size = 0; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); res = curl_easy_perform(curl); ``` 8. 处理响应数据。在这里,我们将响应数据打印出来。 ``` printf("%s\n", response); ``` 9. 释放Curl句柄和请求头。 ``` curl_easy_cleanup(curl); curl_slist_free_all(headers); ``` 完整的代码示例如下: ``` #include <stdio.h> #include <curl/curl.h> size_t write_memory_callback(void *ptr, size_t size, size_t nmemb, void *data) { size_t realsize = size * nmemb; char **mem = (char **)data; *mem = realloc(*mem, response_size + realsize + 1); if (*mem == NULL) { printf("out of memory\n"); return 0; } memcpy(*mem + response_size, ptr, realsize); response_size += realsize; (*mem)[response_size] = 0; return realsize; } int main(void) { CURL *curl; CURLcode res; char *response = NULL; size_t response_size = 0; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/api"); curl_easy_setopt(curl, CURLOPT_POST, 1L); struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/json"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"name\": \"John Smith\", \"age\": 30, \"city\": \"New York\"}"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); res = curl_easy_perform(curl); if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } else { printf("%s\n", response); } curl_slist_free_all(headers); curl_easy_cleanup(curl); free(response); } curl_global_cleanup(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值