本文介绍了Scrapy FormRequest发送JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可以发送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

我不能像

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.

有什么想法吗?

-KM

推荐答案

FormRequest用于模拟HTML表单(例如application/x-www-form-urlencoded).听起来您只是想在Request中发布数据.由于您提到的内容类型为"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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:34