0

I'm having trouble accessing an API with Python. Here's my Python:

import requests

url = 'https://bigjpg.com/api/task/'
payload = {'style': 'photo', 'noise': '3', 'x2': '1', 'input': 'https://www.example.com/photo.jpg'}
headers = {'X-API-KEY': 'xyz123'}

r = requests.get(url, data=payload, headers=headers)

print(r.text)

It returns 404 content but when I use cURL, it works and returns the json I'm looking for. This is the cURL line from the API:

curl -F 'conf={"style": "photo", "noise": "3", "x2": "1", "input": "https://www.example.com/photo.jpg"}' -H 'X-API-KEY:xyz123' https://bigjpg.com/api/task/

2 Answers 2

2

Your problem is that the content passed on the cURL line is an assigned to conf of a JSON object, but you're passing in a Python dict to data.

This should work:

import requests

url = 'https://bigjpg.com/api/task/'
payload = 'conf={"style": "photo", "noise": "3", "x2": "1", "input": "https://www.example.com/photo.jpg"}'
headers = {'X-API-KEY': 'xyz123'}

r = requests.post(url, data=payload, headers=headers)

print(r.text)

And as remarked by @Laurent Bristiel, you need to POST instead of GET.

If you prefer to use a Python dict, you could also do this:

import requests
import json

url = 'https://bigjpg.com/api/task/'
conf = {'style': 'photo', 'noise': '3', 'x2': '1', 'input': 'https://www.example.com/photo.jpg'}
payload = f'conf={json.dumps(conf)}'
headers = {'X-API-KEY': 'xyz123'}

r = requests.post(url, data=payload, headers=headers)

print(r.text)
Sign up to request clarification or add additional context in comments.

1 Comment

I also had to add 'Content-Type': 'application/x-www-form-urlencoded' to my headers. Thanks!
0

The equivalent of a curl -F is a POST rather than a GET:

r = requests.post(url, data=payload, headers=headers)

Comments

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.