cowinapi / developer.cowin

This group is created to facilitate technical and integration discussions related to cowin platform. API related contents can be obtained at API setu portal https://apisetu.gov.in/public/marketplace/api/cowin
115 stars 30 forks source link

Certificate API returns Binary data of the pdf , how to convert it to pdf ?? #259

Open anandvijay094 opened 3 years ago

anandvijay094 commented 3 years ago

after using bearer token & benificiary id i m getting binary data of Vaciination Certificate . how to convert it into pdf ??

idevesh commented 3 years ago

For which platform are you building your software? @anandvijay094

parjanyaacoder commented 3 years ago

How to make a call at Certificate api using bearer token and benifiary Id ? is this the correct way in Flutter.

http.get("/url/beneficary_reference_id?=ID",headers:{ 'Authorization':'Bearer $token' });

idevesh commented 3 years ago

How to make a call at Certificate api using bearer token and benifiary Id ? is this the correct way in Flutter.

http.get("/url/beneficary_reference_id?=ID",headers:{ 'Authorization':'Bearer $token' });

Why are you passing bearer, it can be like 'Authorization': '$token' PS. i have used it in my kotlin app and its working. @parjanyaacoder

anandvijay094 commented 3 years ago

For which platform are you building your software? @anandvijay094

React

kichappa commented 3 years ago

Why are you passing bearer, it can be like 'Authorization': '$token'

The CoWin website sends it as Authorisation: "bearer \<token\>. Since no documentation is provided in API Setu, it is taken as the "standard" way.

parjanyaacoder commented 3 years ago

@kichappa It does not seem to help anyway. I am getting Unauthenticated access in calling certificate API. I tried calling API this way in Flutter

final response = await http.get( Uri.parse('https://cdn-api.co-vin.in/api/v2/registration/certificate/download?beneficiary_reference_id?=$referenceId'), headers: { 'content-type': 'application/pdf', 'responseType': 'blob', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36', 'authorization': 'Bearer $token', }, );

idevesh commented 3 years ago

For which platform are you building your software? @anandvijay094

React

If you just create a .txt file and put all the binary contents in that file and then change extension to .pdf it will be opened

anandvijay094 commented 3 years ago

If you just create a .txt file and put all the binary contents in that file and then change extension to .pdf it will be opened

I already tried this but it's not working .

kichappa commented 3 years ago

@kichappa It does not seem to help anyway. I am getting Unauthenticated access in calling certificate API.

@parjanyaacoder, your URL seems to be wrong.

Uri.parse('https://cdn-api.co-vin.in/api/v2/registration/certificate/download?beneficiary_reference_id?=$referenceId')

It should be

Uri.parse('https://cdn-api.co-vin.in/api/v2/registration/certificate/download?beneficiary_reference_id=$referenceId'),

I used the following code on python,

import requests. pathlib

bearer_token = "your bearer token"

URL = "https://cdn-api.co-vin.in/api/v2/registration/certificate/download?beneficiary_reference_id={}".format(beneficiary_reference_id)
response = requests.get(URL, headers={
    "accept": "application/json",
    "Accept-Language": "en_US",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
    "authorization": "Bearer {}".format(bearer_token)
    })

with pathlib.Path("certificate.pdf") as f:
    f.write_bytes(response.content)

This works fine for me, giving me the certificate as a pdf.

parjanyaacoder commented 3 years ago

@kichappa I have solved this issue. Please check issue #61 and give your inputs. I am stuck on that.

kichappa commented 3 years ago

@kichappa I have solved this issue. Please check issue #61 and give your inputs. I am stuck on that.

@parjanyaacoder, what is the exact problem you are facing. That issue thread is very vague, and I am not sure where you are stuck.

parjanyaacoder commented 3 years ago

WhatsApp Image 2021-05-20 at 10 08 05 PM

Getting this as a pdf. This is what I am getting as response from cowin certificate api. Used file.writeAsBytes to store in a pdf file but getting this as output.

kichappa commented 3 years ago

@parjanyaacoder did you try using the write_bytes() method?

Edit: Also, rename the file to have pdf as its extension. The image shows a filename certificate, without any extensions.

bhawani0642 commented 3 years ago

Hi I am trying to decode the downloaded file in android can someone help me in this, already tried assuming it is of type Base64. If someone know the downloaded file format please let me know.

clinomaniacop commented 3 years ago

@kichappa I have tried your code snippet for generating the certificate. Its shows 401 error continuously. Everything else is correct idk whats happening. Any possibility that the code you pasted has a bug?

shivamPatwal commented 3 years ago

For which platform are you building your software? @idevesh

hey can you provide me your kotlin code of downloading certificate from api

idevesh commented 3 years ago

For which platform are you building your software? @idevesh

hey can you provide me your kotlin code of downloading certificate from api @shivamPatwal

val certFile = File(
PATH,
"certificate" + "_covid19" + ".pdf"             
)
try {
val certFOS = FileOutputStream(certFile)
certFOS.write(t.bytes())                                   // t is the binary response from Cowin API
certFOS.close()
} catch (e: Exception) {
showToast(getString(R.string.some_error_occurred))
}

Use Retrofit to call the API.

shekharkumar86 commented 3 years ago

For which platform are you building your software? @idevesh

hey can you provide me your kotlin code of downloading certificate from api @shivamPatwal

val certFile = File(
                PATH,
                "certificate" + "_covid19" + ".pdf"             
            )
try {
        val certFOS = FileOutputStream(certFile)
        certFOS.write(t.bytes())                                   // t is the binary response from Cowin API
        certFOS.close()
} catch (e: Exception) {
        showToast(getString(R.string.some_error_occurred))
}

Use Retrofit to call the API.

t.bytes() method is not present in kotlin i have use toByteArray method but pdf file is blank size is correct but there is no any content written on the pdf file. Please help me

sablearjun commented 3 years ago

@shekharkumar86 I am also stucked on the same point. If anyone have solved it please help