hello there, please i need help. I have http reque...
# server
a
hello there, please i need help. I have http request model(*.http file from idea ultimate) like this
Copy code
POST 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
Copy code
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 😩
d
not sure if this is it, but you need to specify an authorization scheme before your key in the Auth header. Eg.
Bearer: <key>
a
@dave thanks , but i have not specified it here but in my code i do like
Copy code
Barrier api key
which i got from ibm
d
can you print out the exact request and response that you're sending/receiving?
a
here it is
Copy code
package 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 key
d
no - i mean to print out the exact http request that you're sending from okhttp
and the response
you can then compare them to the .http file that you've got and try to see where the difference is
a
ah,ok let me open intlij
Copy code
//okhttp
{"code":401, "error": "Unauthorized"}
and
Copy code
#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 is
d
that's not the complete request. you need the raw http message, like this:
Copy code
POST /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
for instance, passing that request through an http4k client, we get:
Copy code
***** 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
are you sure you don't need basic auth instead of bearer?
Copy code
www-authenticate: Basic realm="IBM Watson Gateway(Log-in)"
a
@dave sorry for delay, i was downstairs.k let me give you the api key and give a try
d
please don't give me your API key
🙂
a
k, so what i have to do now
d
ok -
well luckily I think I know what the problem is
that service uses basic auth, with the username set to apikey: https://cloud.ibm.com/apidocs/language-translator
Copy code
If 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:
so you need to use the Basic authentication scheme
you can use this to generate the value
a
ok, letme give a try
@dave i really apriciate your help , i have no words to say THANK YOU sorry for westing yor time😃
d
don't be silly! helping people out is what this slack is for! 😄
😃 1