Reputation: 1840
Problem description
I want to send an http post request to an online BLAST website.I inspected the POST requests and saw this:
Request URL:https://p3.theseed.org/services/homology_service
Referrer Policy:no-referrer-when-downgrade
Request Headers
Provisional headers are shown
Accept:application/json
Content-Type:application/x-www-form-urlencoded
Origin:https://www.patricbrc.org
Referer:https://www.patricbrc.org/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Data
{"method":"HomologyService.blast_fasta_to_database","params":[">test
ATAGCTAACAGCATC","blastn","ref.fna","10",50,0],"version":"1.1","id":"27057295081137034"}:
Now I want to do this for several sequences (so replacing the ATAGCTAACAGCATC
). I'm familiar with sending these type of request, however I don't now how to:
form data
, so I can send it using Requestsid
in the post, because I don't know this because it's unique for every BLAST job.Code
import requests as r
blast_url = 'https://p3.theseed.org/services/homology_service'
data = {"method":"HomologyService.blast_fasta_to_database","params":["%3Etest%0ATAGCTAACAGCATC","blastp","ref.faa","10",'50','0'],"version":"1.1"}
headers = {
'Host': 'p3.theseed.org',
'Accept': 'application/json',
'Accept-Language': 'en-US,en;q=0.5',
'Referer': 'https://www.patricbrc.org/app/BLAST',
'Content-Type': 'application/rqlquery+x-www-form-urlencoded',
'X-Requested-With' : 'XMLHttpRequest'
}
res = r.post(blast_url, headers = headers, params = data).text
print(res)
I didn't fill in the id
, but that doesn't seem to be a problem because in the error message the id is filled in (so it seems to generated it automatically?)
This is the error I get:
{"id":"15004153692662703","error":{"name":"JSONRPCError","code":-32700,"message":"You did not supply any JSON to parse in the POST body."},"version":"1.1"}
So obviously the wrong formatting for the form data gives these problems, but I have no idea how I should format this (and if this will fix the problem)
Upvotes: 0
Views: 2122
Reputation: 4821
You got malformed json string as error so remote api is expecting data to be of json format. You need to do
import json
data = json.dumps(data)
res = r.post(blast_url, headers = headers, data = data).text
And make content type of your header as:
headers['Content-Type'] = 'application/json'
Upvotes: 2
Reputation: 267
You should change this line res = r.post(blast_url, headers = headers, params = data).text
to res = r.post(blast_url, headers = headers, data = data).text
Also, before using some tools, please read documentation of this tool, for example, dosc for requests you can find here
Upvotes: 0