Ananiya
11/14/2020, 4:38 PMPOST url
Authorization: key
Content-Type: application/json
{"text": ["Hello, world.", "How are you?"], "model_id":"en-es"}
which work smoothly but after i implement it in okhttp like this below
val json = """{"text": ["Hello, world.", "How are you?"], "model_id":"en-es"}""".trimMargin()
val request = RequestBody.create(JSON,json);
val response = Request.Builder()
.url(url)
.post(request)
.addHeader("Authorization","key")
.addHeader("Content-Type","application/json")
.build()
val answer = client.newCall(response).execute().body()!!.string()
println(answer)
it start throwing error from the api, i felt i have done something wrong in okhttp authorization post, please help 😩dave
11/14/2020, 4:51 PMBearer: <key>
Ananiya
11/14/2020, 5:38 PMBarrier api key
which i got from ibmdave
11/14/2020, 5:39 PMAnaniya
11/14/2020, 5:43 PMpackage server
import com.ibm.cloud.sdk.core.http.HttpMediaType.JSON
import okhttp3.*
class TranslationClient{
fun fetch(){
val client = OkHttpClient()
val json = """{"text": ["Hello, world.", "How are you?"], "model_id":"en-es"}""".trimMargin()
val request = RequestBody.create(JSON,json);
val response = Request.Builder()
.url("<https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/0b9d580a-c497-406d-9ea0-ae0253820616/v3/translate?version=2018-05-01>")
.post(request)
.addHeader("Authorization","Basic apikey $KEY")
.addHeader("Content-Type","application/json")
.build()
val answer = client.newCall(response).execute().body()!!.string()
println(answer)
}
}
i just got un-authorized error from ibm waston server hence in http file works smoothly with the same url and api keydave
11/14/2020, 5:44 PMdave
11/14/2020, 5:44 PMdave
11/14/2020, 5:44 PMAnaniya
11/14/2020, 5:44 PMAnaniya
11/14/2020, 5:57 PM//okhttp
{"code":401, "error": "Unauthorized"}
and
#http file
POST <https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/0b9d580a-c497-406d-9ea0-ae0253820616/v3/translate?version=2018-05-01>
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Content-Length: 158
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Security-Policy: default-src 'none'
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Language: en-US
strict-transport-security: max-age=31536000; includeSubDomains;
x-global-transaction-id: 30d7980502460ff39e67a02edd8fc425
X-DP-Watson-Tran-ID: 30d7980502460ff39e67a02edd8fc425
X-EdgeConnect-MidMile-RTT: 8
X-EdgeConnect-Origin-MEX-Latency: 480
Date: Sat, 14 Nov 2020 17:56:02 GMT
Connection: keep-alive
{
"translations": [
{
"translation": "Hola, mundo."
},
{
"translation": "¿Cómo estás?"
}
],
"word_count": 8,
"character_count": 25
}
Response code: 200 (OK); Time: 3108ms; Content length: 155 bytes
@dave here it isdave
11/14/2020, 5:59 PMPOST /example/index.html?query1=abc&query2=def HTTP/1.1
Host: <http://toolbox.http4k.org|toolbox.http4k.org>
Accept: image/gif, image/jpeg, */*
Content-Type: text/plain
hello
dave
11/14/2020, 6:05 PM***** REQUEST: POST: <https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/0b9d580a-c497-406d-9ea0-ae0253820616/v3/translate?version=2018-05-01> *****
POST <https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/0b9d580a-c497-406d-9ea0-ae0253820616/v3/translate?version=2018-05-01> HTTP/1.1
***** RESPONSE 401 to POST: <https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/0b9d580a-c497-406d-9ea0-ae0253820616/v3/translate?version=2018-05-01> *****
HTTP/1.1 401
connection: close
content-length: 37
content-type: application/json
date: Sat, 14 Nov 2020 18:04:55 GMT
strict-transport-security: max-age=31536000; includeSubDomains;
www-authenticate: Basic realm="IBM Watson Gateway(Log-in)"
x-edgeconnect-midmile-rtt: 11
x-edgeconnect-origin-mex-latency: 31
{"code":401, "error": "Unauthorized"}
Process finished with exit code 0
dave
11/14/2020, 6:06 PMwww-authenticate: Basic realm="IBM Watson Gateway(Log-in)"
Ananiya
11/14/2020, 6:23 PMdave
11/14/2020, 6:24 PMdave
11/14/2020, 6:24 PMAnaniya
11/14/2020, 6:24 PMdave
11/14/2020, 6:25 PMdave
11/14/2020, 6:25 PMdave
11/14/2020, 6:26 PMdave
11/14/2020, 6:26 PMIf you pass in an API key, use apikey for the username and the value of the API key as the password. For example, if the API key is f5sAznhrKQyvBFFaZbtF60m5tzLbqWhyALQawBg5TjRI in the service credentials, include the credentials in your call like this:
dave
11/14/2020, 6:26 PMdave
11/14/2020, 6:27 PMdave
11/14/2020, 6:27 PMAnaniya
11/14/2020, 6:28 PMAnaniya
11/14/2020, 6:47 PMdave
11/14/2020, 6:47 PM