1

I inspected how a request was sent to a website in firefox:

(Unfortunately I had to change the website URL to a fake one to prevent the server form being requested too much).

I tried to do this request in python:

import requests
import json

seq = 'ATGGCAGACTCTATTGAGGTC'

url = 'http://www.test.com'
body = {'QUERY': seq}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(body), headers=headers)
print(r.text)

However when doing this the website says: Empty gene sequence passed for blast analysis. Please enter a valid gene sequence. So that means that the sequence (i.e. QUERY) is not sent correctly to the server. What am I missing here?

(P.s. hopefully missing of the website is not a problem to answer this question, if it is please let me know maybe I can ask to mention their website)enter image description here

CodeNoob
  • 1,988
  • 1
  • 11
  • 33
  • Just realized I'm in a country where IMGUR is not allowed.... will add the screenshot asap! – CodeNoob Aug 15 '19 at 17:26
  • https://stackoverflow.com/questions/15694120/why-does-http-post-request-body-need-to-be-json-enconded-in-python – mad_ Aug 15 '19 at 17:33
  • I don't understand what I should derive from that question? @mad_ – CodeNoob Aug 15 '19 at 17:50
  • @mad_ I think it might be more beneficial for OP to use the `json=data` kwarg rather than the `data=json.dumps(data)` kwarg for json data to be sent, assuming it's not form-encoded – C.Nivs Aug 15 '19 at 17:52
  • I tried that (`json = body` in my case) but still the same "error". How can I see whether it is form encoded or not? @mad_ – CodeNoob Aug 15 '19 at 17:54
  • @mad_ It did work when I did it like `r = requests.post(url, data = body)` – CodeNoob Aug 15 '19 at 18:02
  • @CodeNoob That was my point. Glad it worked – mad_ Aug 15 '19 at 18:35

2 Answers2

0

I am guessing the string / sequence that you are submitting to that particular website is the problem. I ran your sample code against a POST accepting website:

import requests
import json

seq = 'ATGGCAGACTCTATTGAGGTC'

url = 'http://httpbin.org/post'
body = {'QUERY': seq}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(body), headers=headers)
print(r.text)

And got this result, which shows your query properly formed:

{
  "args": {}, 
  "data": "{\"QUERY\": \"ATGGCAGACTCTATTGAGGTC\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "text/plain", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "34", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.22.0"
  }, 
  "json": {
    "QUERY": "ATGGCAGACTCTATTGAGGTC"
  }, 
  "origin": "2.122.222.8, 2.122.222.8", 
  "url": "https://httpbin.org/post"
}
brazosFX
  • 342
  • 1
  • 11
0

Are you sure the it's supposed to be "QUERY=<>?" Cause it could be incorrect formatting of the body. Normally it's in JSON format as in "title: information." Note the ':' rather than the '='

Gooby
  • 621
  • 2
  • 11
  • 32
  • Also as you can see the person below did "Query:<>" rather than "Query=" and got a successful POST – Gooby Aug 15 '19 at 17:41
  • O but he did it to a POST accepting site so I don't know how useful it is cause the main concern is getting a POST accepted by your particular website. – Gooby Aug 15 '19 at 17:42
  • I didn't change how firefox showed me how the request was send and it was using `=` – CodeNoob Aug 15 '19 at 17:51
  • It might be more helpful if we knew what site you're sending the POST to? Unless it's not something you wish to disclose. – Gooby Aug 15 '19 at 18:03