问题描述
我正在尝试创建一个可以发送 content-type:application/json 的 FormRequest.
I am trying to create a FormRequest that can send content-type:application/json.
这是我的尝试:
yield FormRequest("abc.someurl.com", formdata=json.dumps({"referenceId":123,"referenceType":456}), headers={'content-type':'application/json'}, callback=self.parseResult2)
如果我使用json.dumps()
来处理formdata=中的表单数据,我得到的错误是
If I use json.dumps()
to process the form data in the formdata=, the error I get is
"exceptions.ValueError: 需要 1 个以上的值来解包"
我不能像
formdata={"referenceId":123,"referenceType":456}
FormRequest 有效但未被服务器接受.
The FormRequest works but is not accepted by the server.
import requests
import json
result = requests.post(url, json.dumps({"referenceId":123,"referenceType":456}), headers={'content-type':'application/json'})
它在 python 命令提示符下工作,如上所示.
It works from the python command prompt as in the above.
有什么想法吗?
-公里
推荐答案
FormRequest 用于模拟 HTML 表单(例如 application/x-www-form-urlencoded).听起来你只是想用你的请求发布数据.由于您提到了application/json"的内容类型,因此您可能想要执行以下操作:
FormRequest is for simulating an HTML form (e.g. application/x-www-form-urlencoded). It sounds like you are simply wanting to POST data with your Request. Since you mention a content type of 'application/json' you probably want to do something like this:
request = Request( url, method='POST',
body=json.dumps(my_data),
headers={'Content-Type':'application/json'} )
这篇关于Scrapy FormRequest 发送 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!