TOMTOM api calculateReachableRange 'avoidVignette' or 'allowVignette' - python

i am new to using the TOMTOM API but i got it working with the example in the browser without a problem, call:
https://api.tomtom.com/routing/1/calculateReachableRange/50.97452,5.86605/json/?key=[MYKEY]&timeBudgetInSec=3600
in the browser i get my json response with my polygon point. But in python i just get the error stating:
"Invalid request: should contain one of the following elements 'avoidVignette' or 'allowVignette'"
Does anybody have any idea why it works in the browser but gives an error when i use it in python code?
mycode:
request_post = requests.post('https://api.tomtom.com/routing/1/calculateReachableRange/50.97452,5.86605/json/?key=[MYKEY]&timeBudgetInSec=3600')
thanks in advance

I figured it out with the help of the comment of #ForceBru.
I used postman to figure out what the problem was and it seems that if you do not use the link directly in the browser but use it as a real post request you are needed to give it a xml or json body where you need to specifiy:
{"avoidVignette": []}
if you are using json.
If you put this in your post request as body it should work like a charm.
Working code:
requests.post('https://api.tomtom.com/routing/1/calculateReachableRange/50.97452,5.86605/json/?key=[MYKEY]&timeBudgetInSec=3600', json={"avoidVignette": []})
Hope this helps some people forward if they get the same error.

If You are not providing any POST parameters than You can use GET method.
Here is the link to Online Routing API Explorer - link

Related

Cannot scrape using python.requests() but work when loading on browser

I want to scrape data from this page: https://raritysniffer.com/viewcollection/primeapeplanet
The API request works on the browser but returns 403 ERROR when I use python.requests.
requests.get("https://raritysniffer.com/api/index.php?query=fetch&collection=0x6632a9d63e142f17a668064d41a21193b49b41a0&taskId=any&norm=true&partial=true&traitCount=true")
I understand it is possible that I have to pass on specific headers to make it work, but as a python novice, I have no idea how to make it work. Please advise. Thanks!
If you check the response, you can see that the website uses Cloudfare and which indeed returns the 403. To bypass this, try cloudscraper. (be mindful)
import cloudscraper
url = 'https://raritysniffer.com/api/index.php?query=fetch&collection=0x6632a9d63e142f17a668064d41a21193b49b41a0&taskId=any&norm=true&partial=true&traitCount=true'
scraper = cloudscraper.create_scraper(browser = 'firefox')
print(scraper.get(url).text)

Python request send post request with params not as dictionary

So this seems like it would be a simple thing to do but I can't seem to get it to work. I want to send a post request to a local flask server running on my computer. So I use the following line of code to do it
r = requests.post(end_url + '/upload/flash', cookies=cookie, params='title=asdfg&description=asdfg&front1=f&back1=b&number=1&edu_level=&school=&course=&grade=&unit=&chapter=&section=&tag=')
The problem is not with the params I'm passing into the function as they are formatted correctly. The problem is that the flask server is trying to access the "title" arg from the request but can't find it. All of the examples I have seen format the params in a dictionary (ex.
{"title": "asdfg"}
I would imagine that it is possible to send a request the way I am trying but am not sure what I have done wrong.
The problem was actually not with the params. I was mixing up "params" and "data". I also needed to include the headers
headers={"Content-Type": "application/x-www-form-urlencoded"}
Thanks to everyone who help me troubleshoot my code.

requests.post is not giving any response in python?

I'm using Python requests 2.19.1 .
But I'm facing an intermittent issue where I get no response at all when I post to a specific url.
I'm trying to check if the LDAP is giving me the expected output for invalid credentials.
Here's the format:
requests.post('https://oxhp-member.uhc.com/Member/MemberPortal/j_acegi_security_check',
credentials_payload)
that I'm posting
Almost everytime, it works fine. But sometimes, it doesn't give any response for that. Even network issues gives us some response. Right? Why am I not getting any response for the above call.
Is there any existing bug in requests?
Somebody please point me in correct direction.
requests is not responsible for "giving back response". The server you are using requests to post to is.
To see the response you have to keep it in a variable and handle it somehow.
resp = requests.post('https://oxhp-member.uhc.com/Member/MemberPortal/j_acegi_security_check',
credentials_payload)
print(resp.status_code)
print(resp.content)
Whatever resp contains is the responsibility of the server.

Hyperlink embedded in http request

I'm using the jira-python client to add a comment to an issue:
issue_id = 'SB-196'
comment = 'A Slack channel has been created for discussing this escalation: slack://channel?id={}&team={}'.format(new_channel_id, SLACK_TEAM_ID)
jira.comment(issue_id, comment)
The jira-python client is just a wrapper around the JIRA rest api. When I execute the code, I get a 404 error from JIRA which is recieving the following URL:
https://jira.mycompany.com/rest/api/2/issue/SB-196/comment/A%20Slack%20channel%20has%20been%20created%20for%20discussing%20this%20escalation:%0Aslack://channel?id=C123456&team=T123456
I'm assuming this is happening because it's trying to encode the Slack URL in the REST URL and getting confused. What's the correct way to encode the including URL so it doesn't get confused? Thanks!
Klaus D. led me down the right path. It turns out jira.comment() does a GET. In order to POST, you have to call jira.add_comment(). Once I did that, all worked fine (the client library automatically did the url encoding).

Soundcloud API is returning 403 on some tracks

Soundclouds API is returning 403 on some tracks for me. I have tried playing with the raw http endpoints and also the soundcloud api wrapper for python, both have the issue.
https://api.soundcloud.com/tracks/251164884.json?client_id=CLIENT_ID
The above one returns a 403 error while below one works, using same CLIENT_ID obviously
https://api.soundcloud.com/tracks/197355235.json?client_id=CLIENT_ID
Using the library wrapper I get. requests.exceptions.HTTPError: 403 Client Error: Forbidden
import soundcloud
client = soundcloud.Client(client_id=CLIENT_ID)
track = client.get('/resolve', url='https://soundcloud.com/mtarecords/my-nu-leng-flava-d-soul-shake')
https://soundcloud.com/calyxteebee/nothing-left
Another track that also doesn't resolve. Not all tracks have this issue, most work how they always have.
If you go to the Share -> Embed on Soundcloud the track_id will be in there, so I know I am using the correct track_id.
Viewing the http endpoints in browser I get the error.
Failed to load resource: the server responded with a status of 401 (Unauthorized) - https://api.soundcloud.com/favicon.ico
Anyone else run into this issue before?
Using your two examples I get valid results for both
Example 1:
https://api.soundcloud.com/resolve?url=https://soundcloud.com/calyxteebee/nothing-left&client_id=CLIENT_ID
returns
https://api.soundcloud.com/tracks/251164884?client_id=CLIENT_ID
Example 2:
https://api.soundcloud.com/resolve?url=https://soundcloud.com/mtarecords/my-nu-leng-flava-d-soul-shake&client_id=CLIENT_ID
returns
https://api.soundcloud.com/tracks/249638630?client_id=CLIENT_ID
using this url, working perfectly sir. Try this. :D
https://api.soundcloud.com/tracks/TRACK_ID/stream?client_id=CLIENT_ID
I have been investigating this issue for some time now, and I discovered something which at least solves my situation, dunno if it will solve yours.
The Revelation:
If you do a head request with curl (-I option) then it seems to always return with a 200/300 range response.
Why it works: I am streaming Soundcloud tracks with URLs like https://api.soundcloud.com/tracks/TRACK_ID/stream?client_id=CLIENT_ID in an iOS app using FreeStreamer. The stream was failing on exactly those tracks for which curl -v returned 403 for the tracks URL (it returns 401 for the stream URL). So to solve my situation, I perform a head request which gives 302 Found, extract the mp3 URL, and I use that to stream instead of the original URL.
I consider this a bug of the library (since it should be able to handle any 302) and I created an issue for it.

Categories

Resources