0

I am trying to execute the following cURL command using python script. I want to than save the results as text file.

cURL command:

curl -d @myfile.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=xxxx"

Python Script:

import requests
from requests.structures import CaseInsensitiveDict
url = "https://www.googleapis.com/geolocation/v1/geolocate?key=xxxx"
payload = {r"C:\Users\Desktop\myfile.json"}
res = requests.post(url, data=payload)
print(res.text)

Error:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unexpected token.\myfile.json\n^",
    "errors": [
      {
        "message": "Invalid JSON payload received. Unexpected token.\myfile.json\n^",
        "domain": "global",
        "reason": "parseError"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

From the error, i can understand that the way i am providing the json file must be wrong. I tried different ways but error still remains. The json file contain set of parameters required for the API call.

please could any guide here. Also indicate how to save the output as text.

0

1 Answer 1

1

It's close but all you're doing is sending the json file name to the remote HTTP server. You need to open the file first:

import requests
from requests.structures import CaseInsensitiveDict
url = "https://www.googleapis.com/geolocation/v1/geolocate?key=xxxx"
with open("C:\Users\Desktop\myfile.json", 'rb') as payload:
  res = requests.post(url, data=payload)
  print(res.text)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank u for the prompt reply. Just to finish the thread, is there a way to efficiently save the return as text/json file?
It sounds like you need to spend some time with the Python basics, namely around reading and writing files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.