首页 \ 问答 \ POST文件上载导致HTTP 400错误响应(POST file upload causes HTTP 400 error response)

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
更新时间:2023-10-25 10:10

最满意答案

您的数据几乎与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 is 1 in an integer field

相关问答

更多
  • 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 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)
  • 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 ...
  • 你有两个选择。 从外部生成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) 但是在你的数据中,它应该是空间和| 它应该在哪里,所以首先 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)