简体   繁体   中英

How to specify the “Content-Type” and “Accept” on FormRequest?

Using the RequestForm , I need to specify that the Content-Type is application/json; charset=UTF-8 application/json; charset=UTF-8 and Accept is */* . How to do this?

Currently, my code looks like this:

yield scrapy.FormRequest(url='...',
                         formdata={
                             ...
                         },
                         cookies={...},
                         callback=self.parse_second)

Using browser, the request is:

POST /PaginasPublicas/_SBC.aspx/pesquisaLoteIntegracaoTPCL HTTP/1.1
Host: geosampa.prefeitura.sp.gov.br
Connection: keep-alive
Content-Length: 118
Accept: */*
Origin: http://geosampa.prefeitura.sp.gov.br
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36
Content-Type: application/json; charset=UTF-8
Referer: http://geosampa.prefeitura.sp.gov.br/PaginasPublicas/_SBC.aspx
Accept-Encoding: gzip, deflate
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4,ar;q=0.2,de;q=0.2,es;q=0.2,fr;q=0.2,it;q=0.2,ja;q=0.2,pl;q=0.2,tr;q=0.2,zh-TW;q=0.2
Cookie: ASP.NET_SessionId=bvvghxvsxgwzuyaudsqn5m5q

您的请求应如下所示:

yield FormRequest(..., headers={'Content-Type': 'application/json','charset':'UTF-8'})

Scrapy Request has a field headers which is use to define explicit headers. This will work for you.

yield scrapy.FormRequest(url='...',
                     formdata={
                         ...
                     },
                     cookies={...}, headers={'Content-Type': 'application/json','charset':'UTF-8'},
                     callback=self.parse_second)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM