我想使用cURL不仅在HTTP POST中发送数据参数,而且还上传具有特定表单名称的文件。我该怎么做呢?

HTTP Post参数:

Userid = 12345 filecomment =这是一个映像文件

文件上传: 文件位置= /home/user1/Desktop/test.jpg file = image的表单名称(对应PHP端$_FILES['image'])

我认为cURL命令的一部分如下所示:

curl -d "userid=1&filecomment=This is an image file" --data-binary @"/home/user1/Desktop/test.jpg" localhost/uploader.php

我遇到的问题如下:

Notice: Undefined index: image in /var/www/uploader.php

问题是我使用$_FILES['image']在PHP脚本中拾取文件。

如何相应地调整cURL命令?


当前回答

cat test.txt 

文件test.txt内容。

curl -v -F "hello=word" -F "file=@test.txt" https://httpbin.org/post

> POST /post HTTP/2
> Host: httpbin.org
> user-agent: curl/7.68.0
> accept: */*
> content-length: 307
> content-type: multipart/form-data; boundary=------------------------78a9f655d8c87a53
> 
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
* We are completely uploaded and fine
< HTTP/2 200 
< date: Mon, 15 Nov 2021 06:18:47 GMT
< content-type: application/json
< content-length: 510
< server: gunicorn/19.9.0
< access-control-allow-origin: *
< access-control-allow-credentials: true
< 
{
  "args": {}, 
  "data": "", 
  "files": {
    "file": "file test.txt content.\n"
  }, 
  "form": {
    "hello": "word"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "307", 
    "Content-Type": "multipart/form-data; boundary=------------------------78a9f655d8c87a53", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.68.0", 
    "X-Amzn-Trace-Id": "Root=1-6191fbc7-6c68fead194d943d07148860"
  }, 
  "json": null, 
  "origin": "43.129.xx.xxx", 
  "url": "https://httpbin.org/post"
}

其他回答

下面是如何正确转义任意文件名的上传文件与bash:

#!/bin/bash
set -eu

f="$1"
f=${f//\\/\\\\}
f=${f//\"/\\\"}
f=${f//;/\\;}

curl --silent --form "uploaded=@\"$f\"" "$2"

我用这个命令curl -F 'filename=@/home/ yourhomedirectory /file.txt' http://yourserver/upload

cat test.txt 

文件test.txt内容。

curl -v -F "hello=word" -F "file=@test.txt" https://httpbin.org/post

> POST /post HTTP/2
> Host: httpbin.org
> user-agent: curl/7.68.0
> accept: */*
> content-length: 307
> content-type: multipart/form-data; boundary=------------------------78a9f655d8c87a53
> 
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
* We are completely uploaded and fine
< HTTP/2 200 
< date: Mon, 15 Nov 2021 06:18:47 GMT
< content-type: application/json
< content-length: 510
< server: gunicorn/19.9.0
< access-control-allow-origin: *
< access-control-allow-credentials: true
< 
{
  "args": {}, 
  "data": "", 
  "files": {
    "file": "file test.txt content.\n"
  }, 
  "form": {
    "hello": "word"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "307", 
    "Content-Type": "multipart/form-data; boundary=------------------------78a9f655d8c87a53", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.68.0", 
    "X-Amzn-Trace-Id": "Root=1-6191fbc7-6c68fead194d943d07148860"
  }, 
  "json": null, 
  "origin": "43.129.xx.xxx", 
  "url": "https://httpbin.org/post"
}

导致我到这里的问题原来是一个基本的用户错误——我没有在文件的路径中包含@符号,所以curl发布的是文件的路径/名称,而不是内容。Content-Length值因此是8,而不是我期望看到的给定测试文件长度的479。

当curl读取并发布文件时,将自动计算Content-Length头。

curl -i -H "Content-Type: application/xml"——data "@test.xml" -v -X POST https://<url>/<uri/

... < Content-Length: 479 ...

把这贴在这里,以帮助其他的新手在未来。

你需要使用-F选项: -F/——form <name=content>指定HTTP多部分POST数据

试试这个:

curl \
  -F "userid=1" \
  -F "filecomment=This is an image file" \
  -F "image=@/home/user1/Desktop/test.jpg" \
  localhost/uploader.php