POST文件上载导致HTTP 400错误响应(POST file upload causes HTTP 400 error response)
我正在尝试使用Python中的POST请求(请求库)上传文件。 我收到
HTTP 400 error
作为响应。 我认为这是因为我的POST请求不是应该的格式。 任何想法如何将POST请求1转换为2?码:
files = {"file": ("test-file.txt", open("c:/users/johndoe/desktop/test-file.txt", "rb"), "text/plain")} response = webdriver.request("POST", "https://something.com/attachments", files = files)
我目前拥有的(1):
--d9bd23df892242a489b0f638d62502a6 Content-Disposition: form-data; name="file"; filename="test-file.txt" Content-Type: text/plain This is a test file. Regards, John Doe --d9bd23df892242a489b0f638d62502a6--
我应该拥有什么(2):
-----------------------------26789175756830 Content-Disposition: form-data; name="fileName" test-file.txt -----------------------------26789175756830 Content-Disposition: form-data; name="fileSize" 45 -----------------------------26789175756830 Content-Disposition: form-data; name="description" undefined -----------------------------26789175756830 Content-Disposition: form-data; name="file"; filename="test-file.txt" Content-Type: text/plain This is a test file. Regards, John Doe -----------------------------26789175756830--
我认为我需要添加(3):
-----------------------------26789175756830 Content-Disposition: form-data; name="fileName" test-file.txt -----------------------------26789175756830 Content-Disposition: form-data; name="fileSize" 45 -----------------------------26789175756830 Content-Disposition: form-data; name="description" undefined -----------------------------26789175756830
I am trying to upload a file using POST requests in Python (requests library). I get
HTTP 400 error
as a response. I think it is because my POST request is not in the format it should be. Any ideas how to convert POST request 1 into 2?Code:
files = {"file": ("test-file.txt", open("c:/users/johndoe/desktop/test-file.txt", "rb"), "text/plain")} response = webdriver.request("POST", "https://something.com/attachments", files = files)
What I currently have (1):
--d9bd23df892242a489b0f638d62502a6 Content-Disposition: form-data; name="file"; filename="test-file.txt" Content-Type: text/plain This is a test file. Regards, John Doe --d9bd23df892242a489b0f638d62502a6--
What I should have (2):
-----------------------------26789175756830 Content-Disposition: form-data; name="fileName" test-file.txt -----------------------------26789175756830 Content-Disposition: form-data; name="fileSize" 45 -----------------------------26789175756830 Content-Disposition: form-data; name="description" undefined -----------------------------26789175756830 Content-Disposition: form-data; name="file"; filename="test-file.txt" Content-Type: text/plain This is a test file. Regards, John Doe -----------------------------26789175756830--
What I think I need to add (3):
-----------------------------26789175756830 Content-Disposition: form-data; name="fileName" test-file.txt -----------------------------26789175756830 Content-Disposition: form-data; name="fileSize" 45 -----------------------------26789175756830 Content-Disposition: form-data; name="description" undefined -----------------------------26789175756830
原文:https://stackoverflow.com/questions/42582414
最满意答案
您的数据几乎与Well Known Text一样,但事实并非如此。 这是你自己创造的东西吗? 如果是这样,您应该更改程序以WKT格式生成它。 例如根据WKT,你的观点应该是这样的
POINT (1.138007 -129.142777)
但它是
POINT (1.138007,-129.142777)
类似地,线串的正确表示将是
LINESTRING (7.294799 -122.935414, 7.294679 -122.935129)
但是在你的数据中
,
它应该是空间和|
它应该在哪里,
所以首先要做的是转换PostGIS或任何其他空间感知数据库可接受的数据。当你在这里时,请从id中删除id_部分。 将
id_1
保存为varchar是没有意义的,在整数字段中应该保存的是1
Your data is almost like Well Known Text but it isn't. Is this something that you have generated yourself? If so you should change your program to generate it in the WKT format. For example according to WKT your point field ought to look like
POINT (1.138007 -129.142777)
But it's
POINT (1.138007,-129.142777)
Similarly the correct representation for a linestring would be
LINESTRING (7.294799 -122.935414, 7.294679 -122.935129)
But in your data you have
,
where it should be space and|
where it should be,
so the first thing to do is to transform your data that's acceptable to PostGIS or any other spatially aware database.While you are at it, please strip out the id_ part from the id. It's pointless to save
id_1
as a varchar, what should be saved is1
in an integer field
相关问答
更多-
在.NET中批量插入SQL(Batch SQL inserts in .NET)[2022-03-08]
DataAdapter具有UpdateBatchSize属性。 将UpdateBatchSize设置为正整数值会导致数据库更新作为指定大小的批处理发送。 希望这可以帮助... The DataAdapter has a UpdateBatchSize property. Setting the UpdateBatchSize to a positive integer value causes updates to the database to be sent as batches of the spec ... -
这发生在我身上一次。 不知道为什么,但对我来说,它重新启动postgresql服务后已修复。 我的想法是,如果你不重新启动服务(即使在重新启动后),看起来postgres会保留它的WAL(预写日志记录)。 通过重新启动服务,所有事情都会清理干净,所以所有事情都会像新的一样顺畅。 但是,也许还有另外一个原因。 无论如何,请尝试重新启动服务,而不是在重新启动后重新启动或重新启动服务。 阅读问题评论以获取更多信息或@ user35934发布的此链接 。 This happened to me once. Don' ...
-
如何让postgres忽略带有重复键的插入,但继续前进(how to have postgres ignore inserts with a duplicate key but keep going)[2022-11-18]
如果您使用的是Postgres 9.5或更新的版本(我假设您是,自2016年1月发布以来),您可以使用非常有用的ON CONFLICT Cluase: INSERT INTO mytable (id, col1, col2) VALUES (123, 'some_value', 'some_other_value') ON CONFLICT (id) DO NOTHING If you're using Postgres 9.5 or newer (which I assume you are, sinc ... -
使用identity中断批次。 Fabio Maulo 在这里解释 最好的选择是切换到不同的发电机(我总是推荐hilo或guid.comb ) Using identity breaks batching. Explained by Fabio Maulo here The best alternative is switching to a different generator (I always recommend hilo or guid.comb)
-
使用PHP批量插入(Batch inserts with PHP)[2022-04-22]
MySQL本身支持多个插入与一个查询。 INSERT INTO [Table] ([Column List]) VALUES ([Value List 1]) , ([Value List 2]) [...] , ([Value List N]) MySQL natively supports multiple insertions with one query. INSERT INTO [Table] ([Column List]) VALUES ([Value Lis ... -
我不完全理解,但我认为你可以在每一行后得到你的密钥,然后在statament块之后更新数据,如下所示。 declare idx number := 1 ; type array_t is varray(10000) of varchar2(100) ; colorArr array_t := array_t(); AFTER EACH ROW IS BEGIN if inserting then colorArr (idx) := :new.color ...
-
使用postgres的Python使用命名变量和批量插入(Python with postgres using named variables and bulk inserts)[2022-11-29]
你有两个选择。 从外部生成ID(允许您进行批量插入)或从序列生成ID(这意味着您必须执行单个条目插入)。 我认为这非常简单地计算外部ID生成和批量加载(虽然我建议你看一下ETL工具,而不是在python中手工编写代码)。 如果需要从串行中提取ID,则应考虑服务器端预处理语句 。 您的第一个语句应如下所示: dbCursor.execute(""" PREPARE cms_lib_insert (bigint, text, text) AS INSERT INTO cms_libraries (cms_li ... -
在应用限制之前,PostgreSQL在PERFORM查询的每一行上运行函数。 所以即使它只返回10,它仍然会运行10次以上的功能。 解决方案是使用子查询,有趣的是PERFORM不起作用,但SELECT也可以工作。 select fn_debtor_summary( limitclients.clientacc) from (select clientacc from client limit 1) limitclients; PostgreSQL runs functions on every row i ...
-
我不明白这是怎么插入任何东西的! 这条线不正确 $sql2=$connec->prepare("INSERT INTO tbl_naaccr_staging (addr,name, email) VALUES (?, ?, ?"); ^ ^ here 纠正它 $sql2=$connec->prepare("INSERT INTO tbl_naac ...
-
您的数据几乎与Well Known Text一样,但事实并非如此。 这是你自己创造的东西吗? 如果是这样,您应该更改程序以WKT格式生成它。 例如根据WKT,你的观点应该是这样的 POINT (1.138007 -129.142777) 但它是 POINT (1.138007,-129.142777) 类似地,线串的正确表示将是 LINESTRING (7.294799 -122.935414, 7.294679 -122.935129) 但是在你的数据中,它应该是空间和| 它应该在哪里,所以首先 ...