简体   繁体   English

curl post:如何在单个请求中同时发送文件和json数据

[英]curl post: how to send both file and json data in a single request

How to send both file and data in a single curl POST? 如何在单个curl POST中发送文件和数据?

Tried something like using -F and -d option together, but it didn't help: 尝试一起使用-F和-d选项,但没有帮助:

curl  -X POST -F 'file=/Users/<uid>/model.tar.gz' http://<uri>/api/v1/modelfiles/ -H 'AUTH-TOKEN:<token>' -d '{"model_id": "<uuid>",
           "filename": "model.tar.gz",
           "framework": "Tensorflow",
           "framework_version": "1.8.0",
           "meta": {}
}'

Short version: you can't. 简短版:您不能。 That's not how HTTP works. 这不是HTTP的工作方式。 You get to send one blob of data in one POST request, but you are trying to send two (the JSON string and the file). 您可以在一个POST请求中发送一个数据块,但是您尝试发送两个(JSON字符串和文件)。

Longer version: this depends on what your service expects. 较长的版本:这取决于您的服务期望。 Is the data from the file supposed to be a part of the JSON? 文件中的数据是否应该是JSON的一部分? Then you need to preprocess the JSON and put the file data inside, so that you're sending -d '{"file": "<your file data here>", "filename": ... }' 然后,您需要预处理JSON并将文件数据放入其中,以便发送-d '{"file": "<your file data here>", "filename": ... }'

Is the file supposed to be a form field called "file", and the JSON data is contents of a field called "json"? 文件是否应该是名为“ file”的表单字段,并且JSON数据是名为“ json”的字段的内容? Then you can send both using -F file=@/users/uid/model.tar.gz -F 'json={...}' . 然后,您可以使用-F file=@/users/uid/model.tar.gz -F 'json={...}'发送两个文件。 Curl will take care of inlining them into the blob, same as if you had a browser form with two fields. Curl将照顾将它们内联到Blob中,就像您使用带有两个字段的浏览器表单一样。

(note also the @ sign in front of the filename; you need that, otherwise you are sending the string "/users/uid/model.tar.gz") (还要注意文件名前面的@符号;您需要这样做,否则将发送字符串“ /users/uid/model.tar.gz”)

Is it something else? 还有吗 Maybe you're supposed to send the data first and the JSON second? 也许您应该先发送数据,然后再发送JSON?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RequestsLibrary - 如何在同一个 POST 请求中发送数据和文件 - RequestsLibrary - how to send both data and file in the same POST request 我如何在curl请求中发送json数据和文件以由Flask处理? - How can I send a json data along with a file in a curl request to handle by Flask? 如何在 FastAPI POST 请求中同时添加文件和 JSON 正文? - How to add both file and JSON body in a FastAPI POST request? 如何在 FastAPI POST 请求中同时添加文件和 JSON 表单? - How to add both file and JSON Form in a FastAPI POST request? 从同时发送JSON和非JSON数据的python发送POST请求 - send a POST request from python that has both JSON and non-JSON data 如何在 python 中为此 curl 命令发送发布请求 - how to send post request in python for this curl command 发送带有数据和 JSON 参数的 post 请求 - Sending post request with both data and JSON parameters 如何在单个ZMQ发送请求中同时发送图像(ndarray)和字符串数据 - How to send both image(ndarray) and string data in single ZMQ send request 如何在Post请求中发送表格数据以及json参数? - How to send Form data in Post request along with json parameter? 如何在python中使用机械化发送带有后期请求的原始JSON数据 - How to send raw JSON data with a post request using mechanize in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM