0

I am trying to make a request following the IBM Tone Analyzer API instructions, and according to the docs this is what it'll look like.

curl -X POST -u "apikey:{apikey}" --header "Content-Type: application/json" --data-binary @tone.json "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21"

I converted that to a python request() and I have this:

response = requests.post(url=analyzer_url, header=header, data=data)

the problem is that I have no clue what -u "apikey:{apikey}"'s equivalent is on the request() parameters.

Could anyone help me?

lokilindo
  • 682
  • 5
  • 17
  • [first result](http://docs.python-requests.org/en/master/user/authentication/) of the search for "python requests authentication". – Bakuriu Feb 16 '19 at 17:23
  • Possible duplicate of [how to use python to execute a curl command](https://stackoverflow.com/questions/25491090/how-to-use-python-to-execute-a-curl-command) – Jim Todd Feb 16 '19 at 17:23
  • 1
    Maybe a duplicate but [What is the cleanest way to do HTTP POST with basic auth in Python](https://stackoverflow.com/questions/6256126/what-is-the-cleanest-way-to-do-http-post-with-basic-auth-in-python) shows you how to add the `apikey:{apikey}` to your request (which is the username/password for the request). – T Heath Feb 16 '19 at 17:26
  • @Bakuriu Your comment helped the most. Thanks. But I didn't know to look for authentication, I didn't even know it was called that. – lokilindo Feb 17 '19 at 00:29
  • @JimTodd Please read the context of my question... it is not a duplicate. – lokilindo Feb 17 '19 at 00:31
  • Don't get too hang up of being marked as duplicated. All the links worth of reading and learn even it does not provide direct answer. When been marked as duplicate, re-read your question, if you think it is not a duplication, simply clarify to point out what specifically you are looking for. – hcheung Feb 17 '19 at 00:47
  • @lokilindo - Apologies if you got offended. The apikey was also handled in the last answer for the above said link. The comment is constructive one, and not exhaustive. Thanks and Happy learning! – Jim Todd Feb 17 '19 at 05:57

1 Answers1

2

Well I did look at this question but I don't think they talk about the -u "apikey:{apikey}" which was the main source of my issue. (what I now know is Authentication).

I found my answer thanks to Bakuriu's reply (here) My final post request looked like this:

res = requests.post(
url, 
headers=headers, 
data=json.dumps(my_json).encode('utf-8'),

auth=("apikey", API_KEY)
)

(I wish the folks in the comment section had taken the time to read the context of my question rather than looking for a duplicate. @ Bakuriu your response helped me the most, but you just didn't submit an actual answer, so I had to answer my own question.)

lokilindo
  • 682
  • 5
  • 17