|
某网站,采用的是post请求,然后打开f12调试查看:它提交的参数并不是我们熟悉的formdata类型,而是payload,这种是怎么回事呢,先了解下什么是payload https://www.cnblogs.com/Eeyhan/p/12491182.html 这个问题在python中,有很多的文章进行了介绍。
在R中找到一篇,https://www.r-bloggers.com/2019/01/json-payload-for-post-request/
body = '{ "filters": { "keywords": ["money","government"], "award_type_codes": [ "A", "B", "C", "D" ] }, "fields": [ "Award ID", "Mod", "Recipient Name", "Action Date", "Transaction Amount", "Awarding Agency", "Awarding Sub Agency", "Award Type" ], "page": 1, "limit": 35, "sort": "Transaction Amount", "order": "desc" }' # Send the body as a JSON string. POST("https://api.usaspending.gov//api/v2/search/spending_by_transaction/", content_type_json(), body = body) # Send the body as a JSON encoded list. POST("https://api.usaspending.gov//api/v2/search/spending_by_transaction/", body = body_list, encode = "json")
方法是可以借鉴的,但是如果 payload 需要改变参数,甚至是循环变量时。以上的方法有不足。
下面提供我个人的方法,即在R中将 payloadData数据要转换成json,提交请求。
data <- list("id"=ID[i,1], "beginTime"=date-7, "endTime"=date, "prefixTopic"="") apiCode <- "sometext" token <- "alsosometext" data <- data header <- list("token"=token) body <- list("apiCode"=apiCode, "data"= data, "header"=header) # payloadData数据要转换成json payload=toJSON(body,pretty=TRUE,auto_unbox=TRUE) payload #运行结果如下 { "apiCode": "sometext", "data": { "id": 12341234, "beginTime": "2022-11-11", "endTime": "2022-11-16", "prefixTopic": "" }, "header": { "token": "alsosometext" } }
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-3 11:07
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社