Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

python 3.x - Convert Curl Request

would someone be able to convert this curl?

curl -X POST "https://api2.isbndb.com/books" -H "accept: application/json" -H "Authorization: special access key" -H "Content-Type: application/json" -d "isbns=9781492666868,9781616555719"

I did so like this:

import requests
headers = {
    'accept': 'application/json',
    'Authorization': 'special access key',
    'Content-Type': 'application/json',
}

data = {
    'isbns': '9781492666868,9781616555719'
}

response = requests.post('https://api2.isbndb.com/books', headers=headers, data=data)

The results that i get from this are below:

print(response.json())
{'total': 0, 'requested': 1, 'data': []}

I do get the book data results I am looking for when I change my data dict to:

data = {
    'isbns': '9781492666868'
}

So, I am little confused on how to approach this. Any thoughts are appreciated.

question from:https://stackoverflow.com/questions/65928454/convert-curl-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

when you set header Content-Type to application/json, instead of argument data you should use json argument

response = requests.post('https://api2.isbndb.com/books', headers=headers, json=data)

second option is to use data argument with json.dumps

response = requests.post('https://api2.isbndb.com/books', headers=headers, data=json.dumps(data))

your case works when Content-Type is application/x-www-form-urlencoded


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...