I'm trying to use requests in python to post a json dictionary to a url. I need to get a string back from the url but I keep getting a code 141 error -{"code":141,"error":"Missing a github repository link"}. I'm using this website(http://docs.python-requests.org/en/latest/user/quickstart/) to do requests.
Any ideas on why I keep getting that error? Code is below.
import requests
import json
payload = { "email" : "jade#gmail.com", "github" : "https://github.com/"}
headers = {'content-type': 'application/json', "Accept": 'application/json'}
r = requests.post("http://challenge.code2040.org/api/register", params = payload, headers = headers)
print(r.url)
print r.text
Update: The suggestion worked but now I'm getting an{"code":141,"error":"success/error was not called"} error when I try to save the response I recieve from the url into a variable and then post it back to a different url.
#Store the token into a variable
token = r.text
payload = { "token" : token}
headers = {'content-type': 'application/json', "Accept": 'application/json'}
r = requests.post("http://challenge.code2040.org/api/getstring", json = payload, headers = headers)
print r.text
Since you are making a POST request and you need to provide a JSON in the request body, use json argument, not params:
r = requests.post("http://challenge.code2040.org/api/register",
json=payload,
headers=headers)
(tested - got back a token)
Note that json argument was introduced in requests 2.4.2.
Related
I'm trying to access the Letterboxd api to get an authentification token but I keep getting Bad Request feedback. Here's my code:
import requests
import urllib
import json
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
url = 'https://api.letterboxd.com/api/v0/auth/token'
body = {
'grant_type': 'password',
'username': 'myname',
'password': 'mypassword'
}
response = requests.post(
url+urllib.parse.urlencode(headers), data=json.dumps(body))
print('\n')
print(response.text)
Any idea what I did wrong? The response is just an HTTP document with no info besides the Error message "400 Bad Request". Heres the documentation if it helps https://api-docs.letterboxd.com/#auth
First: I can't test if all code works.
You send headers in url but you should send it as part of headers=.
And when you use module requests then you can use json=body instead of data=json.dumps(body)
response = requests.post(url, headers=headers, json=body)
But header 'Content-Type': 'application/x-www-form-urlencoded' suggests that you will send data as form data, not json data - which needs data=body without converting to json
response = requests.post(url, headers=headers, data=body)
EDIT:
In your link to documentation I see link to python module (in part "Example implementations") - so maybe you should use it instead of writing own code because documentation mentionts something about "All API requests must be signed" (in part "Request signing") - and this may need extra code to create it.
I've been trying to make a Post Request to my CRM API. The CRM API is very vague:
"You can use POST request when you wish to create records. You POST a JSON encoded string to the servers and it will return a single instance of the record."
POST /{object_name}
Example:
Request URL (POST):
/accounts
Request Body (JSON):
{
"name": "testing API"
}
I've had plenty of success making GET requests regularly, but POST is not working out so easily.
url = "https://apiv4.reallysimplesystems.com/accounts?<KEY>"
payload = {"name":"Ziggy","owner":"XYZ","addresscounty/state":"Awe","source":"Space"}
headers = {
'Content-Type': 'application/json',
'Cookie': 'XSRF-TOKEN=<TOK>; really_simple_systems_session=<KEY>'
}
response = requests.post(url, headers=headers, data=payload)
I get a status code 200 when I run this, but I'm really looking for that 201. The only clue that I've got to follow at this point is that when I run:
response.json()
I get the error:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I've tried switching the response parameters to json:
response = requests.post(url, headers=headers, json=payload)
I've tried ensuring that my payload is json by using json.dumps():
payload = {"name":"Ziggy","owner":"XYZ","addresscounty/state":"Awe","source":"Space"}
payload = json.dumps(payload)
And I've tried all sorts of other shenanigans that I can't even recall at this point. Does anyone have any idea where I'm going wrong here? The 200 status code makes me feel painfully close.
Replace <AUTH_TOKEN> with your auth token
url = "https://apiv4.reallysimplesystems.com/accounts/"
payload = {"name":"Ziggy"}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer <AUTH_TOKEN>',
}
response = requests.post(url, headers=headers, data=payload)
Problem solved:
import json
import requests
url = "https://apiv4.reallysimplesystems.com/accounts"
payload = {"name":"RSS Test Joe Bloggs","addresscounty/state":"Maryland","source":"Word of Mouth"}
payload = json.dumps(payload)
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer <AUTH KEY>'
}
response = requests.post(url, headers=headers, data=payload)
Rather than using Postman's code which included the in the URL and used :
'Cookie': 'XSRF-TOKEN=<TOK>; really_simple_systems_session=<KEY>'
I replaced it with a standard Authorization header. Secondly, I found that using json.dumps(payload) and json = payload was resulting in a Bad Request.
Could someone take a look at this code and let me know if there's something I'm doing wrong:
import requests
url = "https://api-end-point"
payload = {"grant_type": "client_credentials", "client_id":"my_ic", "client_secret": "not_now_please"}
headers = '{"accept": "application/json", "content-type": "application/json"}'
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
For some reason it's throwing error.
{"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html","title":"Bad Request","status":400,"detail":"JSON decoding error: Syntax error, malformed JSON"}
So first use json instead of data, second don't use ' around the headers, or actually you don't need to specify headers:
import requests
url = "https://api-end-point"
payload = {"grant_type": "client_credentials", "client_id":"my_ic", "client_secret": "not_now_please"}
response = requests.request("POST", url, json=payload)
print(response.text)
The page does not contain json/application but html/text. If you remove the headers then it works:
response = requests.request(method="POST", url=url, data=payload)
If you have special needs to specify in the headers add details to your question
In python I use requests to call an API (cannot share the API itself unfortunately so this is hard to reproduce) in the following way:
import requests
url = url
headers = {'API-key': 'xxxxxxxxxxxxxxxx',
'Content-type': 'application/json',
'Accept': 'application/json'
}
r = requests.get(url, headers = headers, verify=False)
print(r.text)
Here, I think the verify=False forces request to ignore the SSL certificate (as suggested here). This works fine, however I am not able to reproduce it with httr the following way:
library(httr)
url <- url
headers <- c('API-key' = 'xxxxxxxxxxxxxxxx',
'Content-type' = 'application/json',
'Accept' = 'application/json'
))
GET(url = url, add_headers(headers = headers)
Now, I believe that verify=False in the requests code is the key here, someone suggested that the way to ignore SSL certificates with httr is using set_config() before a request:
httr::set_config(httr::config(ssl_verifypeer=0L, ssl_verifyhost=0L))
GET(url = url, add_headers(headers = headers))
But it is not working.
$message
[1] "Unauthorized"
$http_status_code
[1] 401
Is httr::set_config(httr::config(ssl_verifypeer=0L, ssl_verifyhost=0L)) the equivalent to verify=False in a requests call?
The error message you showed is really a response from the server. Is has nothing to do with the SSL certificate so you don’t need to disable that checking at all.
The problem is that the name of the parameter in add_headers is .headers not headers. You just need to do
GET(url = url, add_headers(.headers = headers)
I am trying to execute a get request to a service that can send responses in JSON or xml. In the header, I specify Content-Type 'application/json', but I get a response in xml format. I performed the same query using SOAP UI and received a JSON response.
Maybe something is wrong in my request?
import requests
myheader = {'Content-Type': 'application/json'}
auth = requests.get ("""myurl/authenticate""", auth=('user','password'),
headers = myheader)
url = 'myurl/service'
req = requests.get(url = url,
header = myheader,
cookies=auth.cookies)
print(req.json)
print(req.text)
Error text on 'print(req.json)':
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Problem solved by fixing header:
myheader={'Content-type': 'application/json', 'Accept': 'application/json'}