Python POST request return code 200 but not storing the file

Viewed 627

I wrote a Python script to call a API available at https://gtrendsv4-f2ya73vqjq-uc.a.run.app/request_trends. My issue is that API should generate a CSV file and store it on my GCP. The API is taking 5 string parameters: keyword1, keyword2, start, end and country. When I am using POSTMAN to test the API, I am getting a 200 response code and the csv file is correctly generated and stored. This is my url in POSTMAN: url = "https://gtrendsv4-f2ya73vqjq-uc.a.run.app/request_trends?keyword1=run&keyword2=sun&start=2019-02-01&end=2019-02-12&country=FR"

When I am using my Python code for the same POST request on the same service, it's also returning a 200 response code but no csv file is generated/stored. I tried to hardcore the url directly on my python function and it's working so I think I am just missing something but I can't figure out what.

def request_trends_service():
    current_position = get_position_reader()
    new_position, keyword_1 = read_line_from_csv(current_position)
    new_position, keyword_2 = read_line_from_csv(new_position)
    save_new_position(new_position)

    url = "https://gtrendsv4-f2ya73vqjq-uc.a.run.app/request_trends?"

    payload = {
        "keyword1": keyword_1,
        "keyword2": keyword_2,
        "start": start,
        "end": end,
        "country": country
    }

    r = requests.post(url, params = payload)
    print(r)

    return r.status_code

update (code snipped from Postman export):

import requests

url = "https://gtrendsv4-f2ya73vqjq-uc.a.run.app/request_trends?keyword1=run&keyword2=sun&start=2019-02-01&end=2019-02-12&country=FR"

payload = "{\n    \"keyword1\": \"python\",\n    \"keyword2\": \"java\",\n    \"start\": \"01-01-2019\",\n    \"end\": \"07-01-2019\",\n    \"country\": \"gb\"\n}"
headers = {
  'Content-Type': 'text/plain'
}

response = requests.request("POST", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

**Update 2: I am still trying to fix my issue, I guess even I am getting a 200 response back from my Python function, there is something wrong in my code but not sure what. I tried again in Postman and Postman is saving my csv **

2 Answers

Try removing the "?" at the end of the url.

url = "https://gtrendsv4-f2ya73vqjq-uc.a.run.app/request_trends"

params = {
    "keyword1": "run",
    "keyword2": "sun",
    "start": "2019-02-01",
    "end": "2019-02-12",
    "country": "FR"
}

r = requests.post(url, params = params)

print(r.content)

If your request works in postman, you can try to export it to python from the postman interface :

enter image description here

Then choose "Python" and try the code.

Here is what I get :

import requests

url = "https://gtrendsv4-f2ya73vqjq-uc.a.run.app/request_trends?keyword1=run&keyword2=sun&start=2019-02-01&end=2019-02-12&country=FR"

payload = {}
headers= {}

response = requests.request("POST", url, headers=headers, data = payload)

print(response.text.encode('utf8'))
print(response.status_code)

EDIT:

You can create the url with the encoded parameters from your payload dict with urllib :

import urllib

url = "https://gtrendsv4-f2ya73vqjq-uc.a.run.app/request_trends?"

payload = {
    "keyword1": "keyword_1",
    "keyword2": "keyword_2",
    "start": "start",
    "end": "end",
    "country": "country"
}

parameters = urllib.parse.urlencode(payload)
url += parameters
Related